Problem: I am trying to copy *.pdf from multiple subdirectories. These *.pdf can have the same name and i need to rename them. I also needed to copy the *.pdf by date from January to present but i was just trying to get the copy to work first.
My Attempt:
pushd "S:\Clients\"
for /f "delims=" %%f in ('dir S:\Clients\*.pdf /s /b') do (
if exist "C:\Users\administrator\Desktop\Jan-March\%%f" ( xcopy "S:\Clients\%%f" "C:\Users\administrator\Desktop\Jan-March\%%f" 'dir /s /d )
)
popd
pause
Thank you for any help in advance!
Related
I'm trying to write a batch script to use with Windows command prompt, to read from a .csv filename list, grab and select the files from a folder and copy to a new folder:
for /f "delims=" %%i in (theFile.csv) do (
for /f %%j in ('dir /s /b %theDir%\%%i.*') do (
copy "%%j" "C:\Data"
)
)
This command seems to copy but all the files, what I'm doing wrong?
I suppose you are looking for something this, depending on the structure of your csv file:
#echo off
set "thedir=\\PC\Users\Salvatore\Desktop\BackupUnlock"
for /f "delims=" %%i in ("C:\Users\Salvatore\Desktop\csv1.csv") do for /f %%a in ('dir /b /s /a-d "%%i"') do copy "%%a" "C:\Data" /Y
I want to copy and rename files that are stored in a stack of directories (year, month, day). The files are .ers files and I need them to maintain their extension but to go from file.ers to file_cloud.ers.
Then I would like the files for a given month to be saved in the cloud folder within each month.
Ideally I would do this in the Windows Command Line.
I understand a batch file is a good place to start, and so I have the following - this doesn't include saving in a different location - but I am struggling with just the copy and rename.
for /D %%y in ("C:\Data\20*") do (
pushd "%%~y"
for /D %%m in ("*") do (
pushd "%%~m"
for /D %%d in ("*") do (
pushd "%%~d"
for %%f in (soig_*.ers) do copy "%%~f.ers" "%%~f_cloud.ers"
popd
)
popd
)
popd
)
This might be easier than you are making it. If this echos the correct copy command, remove the echo. You get this by sticking your head way down into the output of FOR /?.
FOR /F "usebackq tokens=*" %%f IN (`DIR /S /B /A:-D "*.ers"`) DO (
echo COPY "%%~f" "%%~dpnf_cloud.%%~xf"
)
The following should work okay for you, (it is supposed to copy all matching .ers files with updated names to the existing cloud directory at the previous level of their tree).
#FOR /D %%Y IN ("C:\Data\20*") DO #FOR /D %%M IN ("%%Y\*"
) DO #FOR /D %%D IN ("%%M\*") DO #IF /I NOT "%%~nxD"=="cloud" FOR %%F IN (
"%%~D\soig_*.ers") DO #COPY "%%F" "%%~M\cloud\%%~nFcloud%%~xF">NUL
I have not included anything to prevent overwrites of files whose names already exist within that cloud directory
I have many files with the following structure:
1969/ar/1.jpg
1969/ar/2.jpg
1969/he/1.jpg
1969/he/2.jpg
1969/en/1.jpg
1969/en/2.jpg
1970/ar/1.jpg
etc...
I want to rename all of them, with one command, to one directory, while their names reflect their original folder location.
1969_ar_1.jpg
1969_ar_2.jpg
1969_he_1.jpg
1969_he_2.jpg
1969_en_1.jpg
1969_en_2.jpg
1970_ar_1.jpg
etc...
Is it possible to do so with one command or a batch file?
Thanks!
You may do that to move the files to the base folder with this command-line:
for /R %a in (*) do #set f=%a& set f=!f:%cd%\=!& move "%a" !f:\=_!
Execute it from the folder that contain the 1969, 1970... folders. IMPORTANT!: Delayed Expansion must be active in order for this line to work, so you must previously activate it executing cmd.exe with /V switch this way: cmd /V.
For example:
>xcopy test backup /s
test\1969\ar\1.jpg
test\1969\ar\2.jpg
test\1969\en\1.jpg
test\1969\en\2.jpg
test\1969\he\1.jpg
test\1969\he\2.jpg
test\1970\ar\1.jpg
7 File(s) copied
>cd test
>dir /B
1969
1970
>for /R %a in (*) do #set f=%a& set f=!f:%cd%\=!& move "%a" !f:\=_!
>dir /B
1969
1969_ar_1.jpg
1969_ar_2.jpg
1969_en_1.jpg
1969_en_2.jpg
1969_he_1.jpg
1969_he_2.jpg
1970
1970_ar_1.jpg
Modify the line this way to move the files to another folder:
for /R %a in (*) do #set f=%a& set f=!f:%cd%\=!& move "%a" "\other\folder\!f:\=_!"
Or via this Batch file:
#echo off
setlocal EnableDelayedExpansion
for /R %%a in (*) do set f=%%a& set f=!f:%cd%\=!& move "%%a" "\other\folder\!f:\=_!"
Run this from the base of the tree that contains all *.jpg files.
Change the target folder to where you want the files to go:
Test it on some samples first.
#echo off
for /f "delims=" %%z in ('dir "*.jpg" /b /s /a-d ') do (
for %%a in ("%%~dpz%\.") do (
for %%b in ("%%~dpa\.") do (
ren "%%z" "%%~nxb_%%~nxa_%%~nxz"
move "%%~dpz\%%~nxb_%%~nxa_%%~nxz" "c:\target\folder"
)
)
)
pause
try this (look at the output and remove the word echo before move, if it is OK):
#echo off &setlocal
for /d %%i in (19* 20*) do (
cmd /c "for /r "%%i" %%j in (*.jpg) do #for %%k in ("%%~dpj.") do #echo move "%%~j" "%%i_%%~nk_%%~nxj""
)
Here is my folder hierarchy:
[Numbers]
[Numbers/12545]
[Numbers/12545/dev]
[Numbers/12545/prod]
[Numbers/32445]
[Numbers/32445/dev]
[Numbers/32445/prod]
...
[Numbers/.....]
[Numbers/...../dev]
[Numbers/...../prod]
I want to copy some text files under the only "[Numbers/...../dev]" folders. How should i do?
I tried the below code and it's not work because it coppies under the all subfolders.
for /r %NUMBER_DIRS% %%d in (.) do (
copy %PROJECT_INPUTS%\*.txt "%%d"
)
Thanks.
Try this:
for /d /r "%NUMBER_DIRS%" %%d in (*DEV) do copy "%PROJECT_INPUTS%\*.txt" "%%~d\*.txt"
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (
'dir /s /b /a:d "\numbers" ^| findstr /i /e "\dev"'
) do ECHO COPY %PROJECT_INPUTS%\*.txt "%%i\"
This will report what the batch PROPOSES to do. Remove the ECHO keyword before the COPY to execute the copy.
Note : you may need to add /y to the copy options if you want to OVERWRITE an existing file in the destination directories.
I presume that you're copying FROM %PROJECT_INPUTS% TO many ...\dev directories.
Just want to ask, how can I replace files in destination directory and subdirectories with the same files from source? Say I want to replace file "X" in destination directory or subdirectory with file "X" from source directory, the script must go through all directories and subdirectories to find file "X" and replace it
Example
Source: "C:\MyPics\New\*.*"
Destination: "C:\MyPics\All\A\*.*"
"C:\MyPics\All\B\1\*.*"
"C:\MyPics\All\B\2\*.*"
"C:\MyPics\All\C\*.*"
Do you have any suggestions to accomplish this?
Hope to hear from you soon.
Thanks
This is untested, but I think I've got the code correct.
If you are looking to replace with a single file from your source, then
#echo off
set "src=C:\MyPics\New\"
set "dst=C:\MyPics\All\"
set "file=X"
for /f "eol=: delims=" %%F in ('dir /b /s "%dst%%file%"') do copy /y "%src%%file%" "%%F"
If you are looking to replace with all files from source, then
#echo off
set "src=C:\MyPics\New\*"
set "dst=C:\MyPics\All\"
for %%S in ("%src%") do (
for /f "eol=: delims=" %%F in ('dir /b /s "%dst%%%~nxF"') do copy /y "%%S" "%%F"
)
You could change the mask in the src definition to be more specific then *