How to loop in a folder with batch file - file

I need to do something with a batch file....
I need to copy a folder to another folder but...
If the new folder exist, I need to verify if the file in new folder exist, then I need to rename the file with «.old» at the end of this file before to copy the new file.
I have a great experience of programming in Java, php etc, but not really with batch file...
I m using a syntax of java/php to explain my problem....
set folderOrigin=d:\test1
set folderFinal=d:\test5
if EXIST %folderFinal% (
for (fileOrigin : folderOrigin){
variableNamefileOrigin = fileOrigin
for (fileFinal : folderFinal){
variableNamefileFinal = fileFinal
if (variableNamefileOrigin == variableNamefileFinal){
newvariable = variableNamefileFinal + ".old"
ren variableNamefileFinal newvariable
xcopy /s /q %folderOrigin%+%variableNamefileOrigin%
%folderFinal%+%variableNamefileFinal%
}
}
}
) else (
xcopy /s /q %dossierOrigine% %dossierDestinataire%
)
pause
Thx everyone !

Assuming that the source directory ("folders" are artifacts in the GUI; the structures in the filesystem are "directories") is %sourceFolder% and the destination directory is %finalFolder%; and also assuming that you need to copy only files (not an entire subtree):
To loop through the files in %sourceFolder% you use a for loop:
for %%f in ("%sourceFolder%\*") do call :copyOneFile "%%~f"
exit /b
In the subroutine :copyOneFile you have the current file as %1. To check whether it exists in %finalFolder% you use if exist, and if so you rename it, but not before checking if the .old file exists already:
:copyOneFile
if exist "%finalFolder%\%~nx1" (
if exist "%finalFolder\%~n1.old" del "%finalFolder\%~n1.old"
ren "%finalFolder\%~nx1" "%~n1.old"
)
Now you can copy the file from the source folder to the destination folder:
copy "%~1" "%finalFolder%
To understand the constructions %~nx1 and so on, use for /?. Note than the second argument of ren must have only the filename, a path is not allowed.
If you need to copy an entire subtree then:
After copying the files, redo with for /d to get directories.
Use the appropriate commands instead of del and copy.

Related

Batch script - check to see if file exists from previous iteration, if not process [duplicate]

I have to create a .BAT file that does this:
If C:\myprogram\sync\data.handler exists, exit;
If C:\myprogram\html\data.sql does not exist, exit;
In C:\myprogram\sync\ delete all files and folders except (test, test3 and test2)
Copy C:\myprogram\html\data.sql to C:\myprogram\sync\
Call other batch file with option sync.bat myprogram.ini.
If it was in the Bash environment it was easy for me, but I do not know how to test if a file or folder exists and if it is a file or folder.
You can use IF EXIST to check for a file:
IF EXIST "filename" (
REM Do one thing
) ELSE (
REM Do another thing
)
If you do not need an "else", you can do something like this:
set __myVariable=
IF EXIST "C:\folder with space\myfile.txt" set __myVariable=C:\folder with space\myfile.txt
IF EXIST "C:\some other folder with space\myfile.txt" set __myVariable=C:\some other folder with space\myfile.txt
set __myVariable=
Here's a working example of searching for a file or a folder:
REM setup
echo "some text" > filename
mkdir "foldername"
REM finds file
IF EXIST "filename" (
ECHO file filename exists
) ELSE (
ECHO file filename does not exist
)
REM does not find file
IF EXIST "filename2.txt" (
ECHO file filename2.txt exists
) ELSE (
ECHO file filename2.txt does not exist
)
REM folders must have a trailing backslash
REM finds folder
IF EXIST "foldername\" (
ECHO folder foldername exists
) ELSE (
ECHO folder foldername does not exist
)
REM does not find folder
IF EXIST "filename\" (
ECHO folder filename exists
) ELSE (
ECHO folder filename does not exist
)
Here is a good example on how to do a command if a file does or does not exist:
if exist C:\myprogram\sync\data.handler echo Now Exiting && Exit
if not exist C:\myprogram\html\data.sql Exit
We will take those three files and put it in a temporary place. After deleting the folder, it will restore those three files.
xcopy "test" "C:\temp"
xcopy "test2" "C:\temp"
del C:\myprogram\sync\
xcopy "C:\temp" "test"
xcopy "C:\temp" "test2"
del "c:\temp"
Use the XCOPY command:
xcopy "C:\myprogram\html\data.sql" /c /d /h /e /i /y "C:\myprogram\sync\"
I will explain what the /c /d /h /e /i /y means:
/C Continues copying even if errors occur.
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.
/H Copies hidden and system files also.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
`To see all the commands type`xcopy /? in cmd
Call other batch file with option sync.bat myprogram.ini.
I am not sure what you mean by this, but if you just want to open both of these files you just put the path of the file like
Path/sync.bat
Path/myprogram.ini
If it was in the Bash environment it was easy for me, but I do not
know how to test if a file or folder exists and if it is a file or
folder.
You are using a batch file. You mentioned earlier you have to create a .bat file to use this:
I have to create a .BAT file that does this:
Type IF /? to get help about if, it clearly explains how to use IF EXIST.
To delete a complete tree except some folders, see the answer of this question: Windows batch script to delete everything in a folder except one
Finally copying just means calling COPY and calling another bat file can be done like this:
MYOTHERBATFILE.BAT sync.bat myprogram.ini

Use xcopy to find and copy file

xcopy supports * wildcards and even allows you to clone whole directory structure. My project uses these Qt libraries I need to distribute:
Qt5CLucene.dll
Qt5Core.dll
Qt5Gui.dll
Qt5Help.dll
Qt5Multimedia.dll
Qt5Network.dll
Qt5PrintSupport.dll
Qt5Sql.dll
Qt5Svg.dll
Qt5Widgets.dll
Qt5Xml.dll
Qt5XmlPatterns.dll
They are not all in the same directory in the Qt installation and the list is subject to change - especially additions. So I wanted to use wildcard /*/ to find a file anywhere in directory tree:
C:\Qt\5.3.0-64> xcopy ".\*\%NAME%.dll" "%~dp0\release"
It doesn't work, files are not being found. This is the full code:
C:
cd C:\Qt\5.3.0-64\
For %%a in (
"Qt5CLucene"
"Qt5Core"
"Qt5Gui"
"Qt5Help"
"Qt5Multimedia"
"Qt5Network"
"Qt5PrintSupport"
"Qt5Sql"
"Qt5Svg"
"Qt5Widgets"
"Qt5Xml"
"Qt5XmlPatterns"
) do (
xcopy ".\**\%%~ad.dll" "%~dp0\debug"
)
So can I somehow avoid typping full paths (eg. qtbase\bin\Qt5CLucene) in the batch?
try like this (xcopy could ask if you want to create directory - copy is used instead):
For /r "C:\Qt\5.3.0-64\" %%a in (
"*Qt5CLucene.dll"
"*Qt5Core.dll"
"*Qt5Gui.dll"
"*Qt5Help.dll"
"*Qt5Multimedia.dll"
"*Qt5Network.dll"
"*Qt5PrintSupport.dll"
"*Qt5Sql.dll"
"*Qt5Svg.dll"
"*Qt5Widgets.dll"
"*Qt5Xml.dll"
"*Qt5XmlPatterns.dll"
) do (
copy /y "%~fa" "%~dp0\debug"
)
If I understood you correctly, you want to copy all *.dll files from C:\Qt\5.3.0-64\ -folder and it's sub folders to a debug sub folder of your script file folder.
You could try something like this:
:: Start of your *.bat or *.cmd file
FOR /R C:\Qt\5.3.0-64\ %%a IN (*.dll) DO (
xcopy "%%~a" "%dp~0debug\"
)
:: End of your *.bat or *.cmd file

batch file command to copy file

if EXIST C:\Users\g511411\Desktop\unsigned\*.tar (for /R C:\Users\g511411\Desktop\unsigned\GNU_ARM_DEBUG %%f in (*.bin) do copy "%%f" C:\Users\g511411\Desktop\dll\Destination
for /R C:\Users\g511411\Desktop\unsigned\GNU_ARM_DEBUG %%f in (*.txt) do copy "%%f" C:\Users\g511411\Desktop\dll\Prm)
I have an unsigned folder which has .tar file and GNU_ARM_DEBUG folder. According to second command in if: if .tar is present in unsigned folder then copy .txt from GNU_ARM_DEBUG folder to Prm folder. But GNU_ARM_DEBUG also has Resources folder and this command also copies .txt file from Resources folder which I don't want. What should I do?
I'd replace your second line with
xcopy "C:\Users\g511411\Desktop\unsigned\GNU_ARM_DEBUG\*.txt" "C:\Users\g511411\Desktop\dll\Prm\"
)
Note that adding /L to the xcopy command will simply display a List of the copies batch proposes to do - good for checking that the command is correct.
You could also add /y to the xcopy to auto-overwrite existing files, and add >nul / 2>nul to the end of the line to suppress reports/error reports.

Exporting multiple TAR's to maintain file structure and names

I've been struggling with this one for a couple days now, as I'm not very code-savvy. Basically, I was given a giant zip file full of reports that someone needs access to. When that's extracted, it has a full directory structure, and all the files are .tar.gz files. I've got a working batch file to extract all .tar.gz to .tar, and then delete the .gz's.
The problem I'm facing now is that inside the TAR's, all the file names are meaningless as a series of numbers. I'm trying to make a .bat that will extract the TAR's and rename the contents to the same file name as the .tar had. (There is only one file per TAR).
Here's what I've got at the moment:
FOR /D /r %%F in ("*") DO (
pushd %CD%
cd %%F
SET
FOR %%X in (*.tar) DO (
set filename="%%X"
"C:\Program Files\7-zip\7z.exe" x "%%X" -aou -o"%%F\temp\"
REN "%%F\temp\*" %filename%
REN "%%F\temp\*.tar" "*.null"
MOVE "%%F\temp\*.null" "%%F"
RMDIR "%%F\Temp\"
DEL "%%X"
)
popd
)
I've tried loading %%F into variables, but that caused another set of problems. I have a final script that uses TrID to identify and correct file types, so the .null value is just temporary.
Test this - it is meant to extract the TAR file, rename the extracted file to the same name as the TAR with .null on the end, and then delete the original TAR file and move the renamed file to where the original TAR file was, removing the temp folder.
It's meant to be run in the main folder with all the TAR files in folders beneath it. Copy some to another folder and try it.
#echo off
FOR /r %%F in (*.tar) DO (
"C:\Program Files\7-zip\7z.exe" x "%%F" -aou -o"%%~dpF\temp\"
REN "%%~dpF\temp\*.*" "%%~nF.null"
DEL "%%F"
MOVE "%%~dpF\temp\%%~nF.null" "%%~dpF"
RMDIR "%%~dpF\temp\"
)

Batch: Copy files where include and exclude conditions are met

My requirement is to write a batch script that will compare the files in two folders. If a file exists in both SourceFolder and TargetFolder, then overwrite the file in TargetFolder with the file in SourceFolder.
Using a for-statement and an if-statement I can achieve this:
for /R %Source% %%G in (Prefix.*.ext) do (
if exist %Target%%%~nxG (
del %%G
copy %Target%%%~nxG %Source%
)
)
Although an additional requirement is to only copy files that start with 'prefix.' and end in '.ext' and also to exclude all files that contain the word 'exclude'.
In English: Copy all files from that source folder that start with 'Prefix.', end in '.ext', does not contain the text 'exclude'. and already exists in the target folder.
This is where I get stuck. Does anyone know how to do this in batch?
You can use xcopy for this. First, I am assuming that Prefix and ext are actual strings, to use variables instead you would have to wrap them like %Prefix%.
Second, you will have to make a new text file. Name it excludes.txt and put it in the same directory as your batch file. (If you don't want to make a batch file, then just put it in the directory that is active when you run the command). The only contents of this file should be your EXCLUDE string with no quotes, or other markup.
Ok, the command itself:
xcopy %Source%\Prefix.*.ext %Target% /U /EXCLUDE:excludes.txt
To break it down:
%Source%\Prefix.*.ext Selects the files in the source folder that start with Prefix and end with .ext
%Target% Specifies the destination for the files
/U Only copy files that already exist in the target directory
/EXCLUDE:excludes.txt This will read in excludes.txt and exclude any file that matches any part of the excludes.txt file.
That's it! This is probably easier than writing a FOR statement with a nested IF.
After reading this SO question, I ended up doing it like this. (Before the question got answered)
pushd %Target%
attrib +h *Exclude
for /R %%G in (Prefix.*.ext) do (
if exist %Target%%%~nxG (
del %%G
copy %Target%%%~nxG
)
)
attrib -h *Exclude
popd
The xcopy solution probably looks better although I'd prefer not to have to create (and remove) files if I can help it.

Resources