How to copy folder with multiple identical file names
I have an encapsulated Windows 10 Kiosk system that creates/provides reports on external USB storage devices (NTFS). Unfortunately there's not a lot I can say about this system as it is fairly well tied down.
When I place that USB storage device on another Windows system (Windows 10 Pro 19044.1387 ) in order to copy/backup I run into some issues.
In one the the report subfolders (images) all the picture files are stored. When attempting to copy this folder to a new location (where nothing preciously exists) I am prompted by Windows to choose to what to do regarding over writing existing files. There shouldn't be any!
Looking in the source folder I can see there are indeed some files with identical names. For example multiple "Image-1.jpg" files. The contents of these files are all visually different. There are no brackets "()" with counts in any of these files.
I originally suspected hidden control characters that the copy was probably stripping away resulting in the duplication, but I could not identify such characters. Not saying this is not the case, I just couldn't easily identify such.
However now I cannot copy the report in its entirety.
What options are left to me to copy/backup this report and maintain the duplicate files with identical names? Can I force Windows to create another file with the same name in the same folder?
UPDATE:
I found the hidden character. It turns out to be a leading " " (space). One file is call "Image-1.jpg" the other is " Image-1.jpg" the Windows copy must be stripping leading spaces and causing a conflict in names where none exist. (Assuming you may start a file name with a leading space)
I just tried to create a txt file from Windows and it doesn't let you create a file with a leading space but from the cmd/dos prompt you can. $ echo abc >" my file.txt" creates a file with a leading space that can be viewed from file explorer.
From Windows File Explorer you cannot rename and remove the leading space as it claims the file names are identical. You must completely give it a new name then change it back to the original name minus the leading space.
So my guess that Windows copy was removing hidden characters is correct. A file name with a leading space matches the same file name without that space so if both are in one folder when that folder is copied they will conflict with each other.
Using the following dos commands to test this issue:
$ mkdir "test"
$ cd test
$ echo "abc" >"file1.txt"
$ echo "def" >" file1.txt"
Then use File Explorer to copy the test folder and you will see it renames one of the files. However it will only automatically rename the file if there is no other conflict with "- Copy". If there is a conflict then you will get prompted what to do.
Solution 1:
You seem to have run into a File Explorer bug. However, PowerShell 7.x and Windows PowerShell don't seem to have this bug.
I created a "Test" folder on my Desktop, containing two files: " 1.jpg
" and " 1.jpg
". Then, I copied the contents of "Test" into "Test2" using the following command:
Set-Location '~\Desktop'
Copy-Item 'Test\*' 'Test2'