Reputation: 23
I currently have a lot of files with the same name stored in many different project folders. I would like to move these files into a new folder, and I'd like to name that folder with the original location of the file.
I currently have this structure:
C:\XYZ\Folder 1\File1.txt
C:\XYZ\Folder 2\File1.txt
C:\XYZ\Folder 3\File1.txt
And I would like all the File1.txt
files to be moved to new folders as below:
F:\Destination\C_XYZ_Folder 1\File1.txt
F:\Destination\C_XYZ_Folder 2\File1.txt
F:\Destination\C_XYZ_Folder 3\File1.txt
I've found it difficult to find and understand what I'm looking for. I can move one file, but beyond that I get prompted to replace the file that was just moved since they all have the same name, and I haven't been able to combine that with creating a new folder with the file location as the name.
Ultimately what I'm trying to do is move multiple files with the same name located in different folders into a new location, but still be aware of the original location of each file. Renaming the file is OK but my file paths are quite long.
Upvotes: 2
Views: 2109
Reputation: 5504
I assume that in C:\
there is a folder XYZ
where there are lots subfolders and some of them (or all of them) have File1.txt
to make a folder and move it there, you may need:
@echo off
setlocal EnableDelayedExpansion
for /R "C:\XYZ\" %%A IN (File1.txt) do (
rem /* Find path of file excluded filename (dp=drive and path): */
set "drive_path=%%~dpA"
rem /* In this %%~dpA, replace '\' and ':\' according to OP's requirements: */
set "formatted=!drive_path:\=_!" & set "formatted=!formatted::=!"
rem /* Make the folder: */
md "F:\Destination\!formatted!"
rem /* Move the file there: */
move "%%~fA" "F:\Destination\!formatted!"
)
The above code made the path in format F:\Destination\C_XYZ_etc\File1.txt
. As mentioned in comments you may also want:
@echo off
setlocal EnableDelayedExpansion
for /R "C:\XYZ\" %%A IN (File1.txt) do (
rem /* Find path of file excluded filename (dp=drive and path): */
set "drive_path=%%~dpA"
rem /* In this %%~dpA, replace '\' and ':\' according to OP's requirements: */
set "formatted=!drive_path:\=!" & set "formatted=!formatted::=!"
rem /* Make the folder: */
md "F:\Destination\!formatted!"
rem /* Move the file there: */
move "%%~fA" "F:\Destination\!formatted!"
)
where it will be in format F:\Destination\CXYZETC\File1.txt
.
If there multiple files you want to check: (using set /p
[input from user]):
@echo off
setlocal EnableDelayedExpansion
:files
set /p files=Please enter the files you want to check separated by spaces. Quote all filenames:
if not defined files (goto:files)
:loop
rem Loop through user input (filenames):
for %%A IN (%files%) do (
for /R "C:\XYZ\" %%B IN ("%%A") do (
rem /* Find path of file excluded filename (dp=drive and path): */
set "drive_path=%%~dpB"
rem /* In this %%~dpB, replace '\' and ':\' according to OP's requirements: */
set "formatted=!drive_path:\=!" & set "formatted=!formatted::=!"
rem /* Make the folder: */
md "F:\Destination\!formatted!"
rem /* Move the file there: */
move "%%~fB" "F:\Destination\!formatted!"
)
)
With arguments (easier):
@echo off
setlocal EnableDelayedExpansion
:argument_check
if [%1] == [] (echo Action requires arguments^^! Please rerun from cmd specifying arguments^^! Remember to quote each filename^^! & exit /b 1)
:loop
rem Loop through arguments (filenames):
for %%A IN (%*) do (
for /R "C:\XYZ\" %%B IN ("%%A") do (
rem /* Find path of file excluded filename (dp=drive and path): */
set "drive_path=%%~dpB"
rem /* In this %%~dpB, replace '\' and ':\' according to OP's requirements: */
set "formatted=!drive_path:\=!" & set "formatted=!formatted::=!"
rem /* Make the folder: */
md "F:\Destination\!formatted!"
rem /* Move the file there: */
move "%%~fB" "F:\Destination\!formatted!"
)
)
Upvotes: 1
Reputation: 56155
xcopy has the /s
switch to recursively scan subfolders. The following recreates the folderstructure (just the folders, where file1.txt
is present) to F:\Destination\C\
:
xcopy /s "C:\XYZ\file1.txt" "F:\Destination\C\"
copies
C:\XYZ\Folder 1\File1.txt
C:\XYZ\Folder 2\File1.txt
C:\XYZ\Folder 3\File1.txt
C:\XYZ\File1.txt
C:\XYZ\Folder 1\Subfolder\File.txt
to
F:\Destination\C\XYZ\Folder 1\File1.txt
F:\Destination\C\XYZ\Folder 2\File1.txt
F:\Destination\C\XYZ\Folder 3\File1.txt
F:\Destination\C\XYZ\File1.txt
F:\Destination\C\XYZ\Folder 1\Subfolder\File1.txt
Upvotes: 0