Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply setCaseSensitiveInfo recursively to all folders and subfolders

Tags:

powershell

I am trying to configure my dotnet core project (in Windows) as "case sensitive", so it behaves as in my production server (linux).

I have found this way of doing it:

fsutil.exe file setCaseSensitiveInfo "C:\my folder" enable 

The problem is that this function is not recursive:

The case sensitivity flag only affects the specific folder to which you apply it. It isn’t automatically inherited by that folder’s subfolders.

So I am trying to build a powershell script that applies this to all folders and subfolders, recursively.

I have tried googling something similar and just modifying the command line, but I don't seem to find the corrent keywords. This is the closest that I've gotten to this sort of example.

like image 926
Xavier Peña Avatar asked Jul 30 '18 09:07

Xavier Peña


1 Answers

Correct code:

(Get-ChildItem -Recurse -Directory).FullName | ForEach-Object {fsutil.exe file setCaseSensitiveInfo $_ enable} 

Explanation:

NOTE: The code in the answer assumes you're in the root of the directory tree and you want to run fsutil.exe against all the folders inside, as it's been pointed out in the comments (thanks @Abhishek Anand!)

Get-ChildItem -Recurse -Directory will give you list of all folders (recursively).

As you want to pass their full path, you can access it by using .FullName[1] (or more self-explanatory | Select-Object -ExpandProperty FullName ).

Then you use ForEach-Object to run fsutil.exe multiple times. Current file's FullName can be accessed using $_ (this represents current object in ForEach-Object)[2].

Hint:

If you want more tracking of what's currently being processed you can add the following to write the path of currently processed file to the console: ; Write-Host $_ (semicolon ; is to separate from fsutil invocation) as it was pointed out in the comments (thanks Fund Monica's Lawsuit !)


[1] .FullName notation works for PowerShell 3.0 and greater, Select-Object -ExpandProperty FullName is preferred if there's a chance that lower version will be used.

[2] $_ is an alias for $PSItem

like image 199
Robert Dyjas Avatar answered Sep 28 '22 03:09

Robert Dyjas