I'm trying to copy some files from one directory to two other directories, using batch.
First i'm making 3 directories and after that, i want to copy the following files to backup1 and backup2.
The files are named 010101.txt - 300101.txt (To backup1) and 010102.txt - 300102.txt (backup2).
mkdir backup1
mkdir backup2
mkdir backup3
copy 1.txt C:\User\Test\Backup1
copy 2.txt C:\User\Test\Backup2
I guess i have to use wildcard somehow, but if i write ?????1.txt and ?????2.txt i get an syntex error.
Try this out:
#echo off
setlocal enabledelayedexpansion
cd /d "C:\Temp\copytest"
set "b1=C:\Temp\Backup1"
set "b2=C:\Temp\Backup2"
for /l %%a in (1,1,300102) do (
set num=%%a
if %%a GTR 10000 if %%a LSS 100000 set num=0%%a
if !num:~-1! EQU 1 (
if exist !num!.txt echo copy !num!.txt %b1%
) ELSE (
if !num:~-1! EQU 2 (
if exist !num!.txt echo copy !num!.txt %b2%
)
)
)
Change paths where applicable. Remove the echos after verifying the output is correct to do the actual copy.
Edit: Simpler way
Copy *1.txt "C:\User\Test\Backup1"
Copy *2.txt "C:\User\Test\Backup2"
#ECHO OFF
SETLOCAL
SET "sourcedir=."
FOR %%b IN (1 2) DO (
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.txt" ^| find /i "%%b.txt" '
) DO (
XCOPY "%sourcedir%\%%a" "c:\user\test\backup%%b\" >nul
)
)
GOTO :EOF
I've assumed you want all files in the directory that contain 1.txt to be copied to ...\backup1 and those that contain 2.txt to ...\backup2.
I used my current directory for testing. You'd need to change the value assigned to 'sourcedir' to suit yourself.
Note that the xcopy will create the destination directory if necessary.
Related
Under the path U:\test\0014* I have 99 folders and each of them has respectively 2 subfolders MASTER and DERIVATIVE_COPY. With a following script I try to count the number of PDFs in MASTER folder. If there are only one .pdf file there I want to copy it to DERIVATIVE_COPY folder. IF there are 0 or >1 .pdf in MASTER I want only to show the number of them. This operation should be done for each of the 99 folders.
#echo off
setlocal enabledelayedexpansion
for /R U:\test\0014\*\MASTER %%i in (*.pdf) do (
set /a anzahl+=1
)
if !anzahl! EQU 1 ( echo !anzahl! )
if NOT !anzahl! EQU 1 ( echo !anzahl! )
pause
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=U:\sourcedir"
for /f %%i in ('dir /ad /s/b "%sourcedir%\master*"') do IF /i "%%~nxi"=="MASTER" (
SET /a found=0
FOR %%x IN ("%%i\*.pdf") DO SET /a found+=1
IF !found!==1 (
XCOPY /y "%%i\*.pdf" "%%i\..\derivative_copy\" >nul
) ELSE (
ECHO !found! .pdf files found IN "%%i"
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
First, execute a dir to get a directory-list of the "files" names that start master in the specified tree. Use /ad to select only the directory-names. Accept only those names where the "name+extension" is master (disregarding case)
For each directory-name found, set found to 0 then increment found for each .pdf file found in the directory %%i.
If the resultant count in found is 1, xcopy the file found to the destination subdirectory (which conveniently creates the subdirectory if it doesn't already exist), using /y to overwrite any existing file of the same name and >nul to make the process silent.
Otherwise, report the directoryname and count of files.
I'm looking to create a bat file to compare local folders from a dir command with folders on a server, and then delete the local folders if they do not exist on the server. Additionally, it is having issues with directories including spaces, but I have not looked into this yet.
My current code is not working correctly. Could anyone provide some guidance please?
Code:
REM Search local directories for files, delete if not present on server
set n=0
set count=0
for /f %%a in ('dir /a:d /b %_Entry_Local_Status60_path%') do (
set folder[!n!]=%%a
set /A a+=1
set /A n+=1
set /A count+=1
)
set n=0
for /L %%a in (0,1,%count%) do (
echo !folder[%n%]!
if not exist %_Entry_Network_Status60_path%\!folder[%n%]! rmdir %_Entry_Local_Status60_path%\!folder[%n%]!
set /A n+=1
)
I use the following to compare files within different folders and delete the ones from first folder which does not exists on second one. With some adaptation probably you could use something similar with folders and not files. This is based on FC and you have to provide the two absolute paths:
echo off
set "Folder1=path\to\Folder1"
set "Folder2=path\to\Folder2"
for /f "delims=" %%F in ('dir /b "%folder2%"') do (
if not exist "%folder1%\%%F" (
fc /b "%folder1%\%%F" "%folder2%\%%F"
if "%errorlevel%" EQU "1" (
del "%folder1%\%%F" && echo Deleted "%%F"
)
) else (
del "%folder1%\%%F" && echo Deleted "%%F"
)
)
pause
Exit
You should take a look at the robocopy command. I think robocopy _source_ _target_ /purge does exactly what you want.
I have the multiple images in sub folder, I don't know how many folders are there. I want to Batch script to find listed names in all the folders and copy the images to destination folder. I tried below script but am getting file not found error.
#echo off
rem Find files and copy files
setlocal EnableExtensions EnableDelayedExpansion
set "SourceBaseFolder=D:\System backup\picture batch file\Test\15oct2015"
set "TargetBaseFolder=C:\OutputFolder"
if not exist "%SourceBaseFolder%\*" (
echo %~nx0: There is no folder %SourceBaseFolder%
set "ErrorCount=1"
goto HaltOnError
)
cd /D "%SourceBaseFolder%"
if not exist "FileNames.txt" (
echo %~nx0: There is no file %SourceBaseFolder%\FileNames.txt
set "ErrorCount=1"
goto HaltOnError
)
set "ErrorCount=0"
for /F "usebackq delims=" %%N in ("FileNames.txt") do (
for /R %%J in ("%%N*") do (
set "FilePath=%%~dpJ"
if "!FilePath:%TargetBaseFolder%=!" == "!FilePath!" (
set "TargetPath=%TargetBaseFolder%\!FilePath:%SourceBaseFolder%\=!"
md "!TargetPath!" 2>nul
if exist "!TargetPath!\*" (
echo Copying file %%~fJ
copy /Y "%%~fJ" "!TargetPath!" >nul
) else (
set /A ErrorCount+=1
echo Failed to create directory !TargetPath!
)
)
)
)
:HaltOnError
if %ErrorCount% NEQ 0 (
echo.
pause
)
endlocal
Can any one fix this problem? Thanks in advance.
The solution you're trying to get to work seems excessively convoluted
I believe something similar to the following should do what you want
FOR /F %%G IN ('dir /a-d /s /b "D:\System backup\picture batch file\Test\15oct2015"^|findstr /I /E /G:"D:\System backup\picture batch file\Test\15oct2015\FileNames.txt"') DO (
copy "%%G" "C:\OutputFolder"
)
To ensure the filename matches are exact, all filenames in filenames.txt should be preceded by backslash, e.g.
\filename1.file
\filename2.file
You can easily generate such a file within a batch file, if necessary
I want to create a batch file to compare two folders with the same set of files and copy any files that are different in size from one folder to the other. I tried to adapt the answer from another question as follows as a starting point, but even this doesn't work.
#echo off
Set folder1="C:\folder1"
Set folder2="C:\folder2"
Cd /D "%folder1%"
For %%a in (*.*) do (
For %%b in ("%folder2%\%%a") do (
If "%%~Za" neq "%%~Zb" echo Different file size in %%a
)
)
Update1:
I figured out what was wrong in the example above; it was the quotes around the directories in the set folder commands. The following works as it should:
#echo off
Set folder1=C:\folder1
Set folder2=C:\folder2
Cd /D "%folder1%"
For %%a in (*.*) do (
For %%b in ("%folder2%\%%a") do (
If %%~za neq %%~zb echo Different file size %%a
)
)
Update2:
So this finally does what I want (compares two folders with the same set of files and copies any files that are different in size from one folder to the other):
#echo off
Set folder1=C:\folder1
Set folder2=C:\folder2
Cd /D "%folder1%"
For %%a in (*.*) do (
For %%b in ("%folder2%\%%a") do (
If %%~za neq %%~zb xcopy "%folder1%\%%a" "%folder2%" /y
)
)
The classic way to do this is
xcopy /L /y /d "directory1\*" "directory2"
The /L LISTS the files that would be copied. /d actually says "if the source file in the first parameter is later than the file in the second" which may not be what you want. The /y prevents a prompt for ovewrite (with the /L switch, just a listing is created, but still...)
Files existing in the source but not in the destination will be copied to the destination (or listed if the /L switch is used)
Remove the /L switch to actually perform the copy.
Add /s to repeat for all subdirectories of the source.
I have this batch file now that copies example.jpg from C:\Temp to the Test-folder and all its subfolders.
I would like it to copy the file only 1 subfolder deep in the Test-folder.
For example copy the picture to Test\subfolder but NOT to Test\subfolder\subfolder2
#echo off
for /r "C:\Temp\Test" %%f in (.) do (
copy "C:\Temp\example.jpg" "%%~ff" > nul
)
PAUSE
Don't use recursion if you want to go only 1 level deep. Try this instead:
#echo off
set src=C:\Temp\example.jpg
set dst=C:\Temp\Test
copy "%src%" "%dst%" >nul
for /d %%d in ("%dst%\*") do (
copy "%src%" "%%~fd" >nul
)
#ECHO OFF
SETLOCAL
SET destroot=c:\temp
FOR /f "delims=" %%i IN ( ' dir /ad/b "%destroot%"' ) DO ECHO COPY "c:\temp\example.jpg" "%destroot%\%%i\"
Simply shows what the batch PROPOSES to do. Remove the ECHO keyword to activate the copy and add >nul to suppress the "copied" message