'How to move file from another directory to created folder that make by name file?
I already make script file for make new folder base on file name and move 1 file to this folder. The code as below.
@ECHO OFF
setlocal EnableExtensions DisableDelayedExpansion
set "sourcedir=C:\Users\XXX\4_MapIdentifyAndCSV"
set "destdir=C:\Users\XXX\5_OldData"
for /F "eol=| delims=" %%A in ('dir /B /A-D-H "%SourceDir%\*.csv" 2^>nul') do (
for /F "eol=| tokens=1 delims=." %%B in ("%%~nA") do (
md "%DestDir%\%%B" 2>nul
move /Y "%SourceDir%\%%A" "%DestDir%\%%B\"
)
)
endlocal
But I want to move another files from another directory to created folder too.
Example: At C:\Users\XXX\2_Video move all file video to crated folder.
At C:\Users\XXX\Sub_Folder move all of Sub_Folder to crated folder.
And at C:\Users\XXX\3_Txt move all Text file to crated folder.
Please supporting me.
Solution 1:[1]
@ECHO OFF
setlocal EnableExtensions DisableDelayedExpansion
set "sourcedir=U:\Users\XXX"
set "destdir=U:\Users\XXX\5_OldData"
FOR %%s IN ("4_MapIdentifyAndCSV:.csv" "2_Video" "Sub_Folder" "3_Txt") DO FOR /f "tokens=1,* delims=:" %%t IN (%%s) DO (
for /F "delims=" %%A in ('dir /B /A-D-H "%SourceDir%\%%t\*%%u" 2^>nul') do (
for /F "tokens=1 delims=." %%B in ("%%~nA") do (
ECHO md "%DestDir%\%%B" 2>nul
ECHO move /Y "%SourceDir%\%%t\%%A" "%DestDir%\%%B\"
)
)
)
GOTO :EOF
I use U: for testing.
The eol=| is not required in your code, so I removed it.
The extra "shell" using %%s..%%u works this way:
The quoted string from the list is assigned to %%s. Each entry in the list is "subdirectoryname:filemask". : is chosen because it can't appear in either a subdirectory name or a filemask. The for ... delims=:... assigns the subdirectory name to %%t and the filemask to %%u. If the filemask is not provided, %%u will be empty.
From there, it's just a matter of piecing together the appropriate parts of the filenames and directories required, inserting * before the filemask to specify "all".
In this way, you can control the filetypes moved if required. It would appear that you want only the .csv files from one directory and all files from the others. You could also specify "4_MapIdentifyAndCSV:.csv" "4_MapIdentifyAndCSV:.xyz" within the list to move all .csv and all .xyz files if you wanted that.
Please note that the md and move commands ar merely echoed for testing purposes. Remove the echo keyword from each of those lines to activate the create/move.
I am not sure that I understand the requirements.
By modifying the list so that each element is
source_subdir:destination_subdir:filemask
we can achieve
4_map...*.csv -> 5_olddata\filename\filename.csv
2_vid...* -> 5_olddata\dest...2_vid...*
sub_f...* -> 5_olddata\dest...sub_f...*
3_txt...* -> 5_olddata\dest...3_txt...*
Of course, the actual destination subdirectory could be common if required - all that would be required is that the second element becomes common.
The .csv destination for the 4_map... source is derived from OP's original code.
Note that the destination_subdir must be a single space as a sequence of delimiters is processed as a single delimiter (: = :: = :::::::)
@ECHO OFF
setlocal EnableExtensions DisableDelayedExpansion
set "sourcedir=U:\Users\XXX"
set "destdir=U:\Users\XXX\5_OldData"
FOR %%s IN ("4_MapIdentifyAndCSV: :.csv" "2_Video:dest_for_2_video" "Sub_Folder:dest_for_sub_folder" "3_Txt:dest_for_3_text") DO FOR /f "tokens=1,2,* delims=:" %%t IN (%%s) DO (
for /F "delims=" %%A in ('dir /B /A-D-H "%SourceDir%\%%t\*%%v" 2^>nul') do (
IF "%%u"==" " (
rem if second element of list item is "empty", destination is directory ...\filename
for /F "tokens=1 delims=." %%B in ("%%~nA") do (
ECHO md "%DestDir%\%%B" 2>nul
ECHO move /Y "%SourceDir%\%%t\%%A" "%DestDir%\%%B\"
)
) ELSE (
rem if second element of list item is not "empty" it is the destination directory ...\%%u
ECHO md "%DestDir%\%%u" 2>nul
ECHO move /Y "%SourceDir%\%%t\%%A" "%DestDir%\%%u\"
)
)
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
