Batch if exist with args in a loop - batch-file

I am trying to move differents files into specific folder through a loop.
For each file, I set modalite, serie_no and serie_name and want to create a specific folder based on those attributes. The file will be also moved to this folder.
But the IF EXIST command fail (The syntax is not correct).
#echo on
setlocal enabledelayedexpansion
set "arg1=%~1"
cd /d D:\!arg1!
for /r %%i in (*.*) do (
REM ...
REM stuff to set modalite, serie_no and serie_name
REM ...
if exist D:\!arg1!\!modalite!(
cd D:\!arg1!\!modalite!
) else (
mkdir D:\!arg1!\!modalite!
cd D:\!arg1!\!modalite!
)
if exist D:\!arg1!\!modalite!\!serie_no!_!serie_name!(
move %%i D:\!arg1!\!modalite!\!serie_no!_!serie_name!
) else (
mkdir D:\!arg1!\!modalite!\!serie_no!_!serie_name!
move %%i D:\!arg1!\!modalite!\!serie_no!_!serie_name!
)
)
I tried to use "modalite","serie_no" and "serie_name" but still have the error
Can I use the if exist command in a for loop in that way? Or should I use for /f %%a in () do ()?
Or maybe there is another way to proceed? I am open to everything.

Related

simple batch script for tidy up folder

I put a for loop as below to my PC desktop for example, in order to move all files to a corresponding folder named by its extension.
for %%G in (*.*) do (
md "%%~xG" 2>nul
move "%%G" "%%~xG"
)
So far the script is ok just I want to improve it with the followings,
make folder only if it is not exist
make folder by parameter expansion will create a folder .jpg, but what i wanted is jpg, can I use something like set x=%%~xG, with setlocal and EnabledDelayExpansion, and then !x:~1! to trim the dot
Don't move the batch file itself to the bat folder created. Shall I use expansion like %f0 ?
Any help would be appreciated.
setlocal enabledelayedexpansion
for %%a in (*.*) do (
if not "%%~xa"=="" (
set "ext=%%~xa" & set "ext=!ext:~1!"
if not exist "!ext!" mkdir "!ext!" 2>nul
if not "%%~fa"=="%~f0" (
move "%%a" "!ext!\%%a" >nul
)
)
)
endlocal
While writing, I realised #Stephan 's comment is identical to this answer, so precedence to him!

Batch Script to find a files inside all Sub Folders

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

Cycle files from different folders inside a batch

In a .bat file, I've something like this:
set Files="..\DataSet\*.shp"
for /r %%i in (%Files%) do (
echo Preparing table %%i
REM other commands
)
This allows me to perform operations with every file with extension .shp inside the folder set in `%Files%.
the problem is that this is a single folder. I'd like to define different folders in the same variables, and cycle all files in them. Something like
set Files="..\DataSet1\*.shp;..\DataSet2\*.shp;..\DataSet3\*.shp"
for /r %%i in (%Files%) do (
echo Preparing table %%i
REM other commands
)
This way I should able to process every .shp file present in all folders defined in the %Files% variable. How can I change my code in order to cycle all files of specific extension in all folders defined in my variable?
#echo off
setlocal enableextensions disabledelayedexpansion
set folders="..\DataSet1" "..\DataSet2" "..\DataSet3"
set extensions=".shp"
for %%f in (%folders%) do for %%x in (%extensions%) do pushd "%%~ff" && (
for /r %%i in ("*%%~x") do (
echo Preparing table %%i
rem .....
)
popd
)
As there is no way to include the folder being iterated by for command into the /r clause of an inner for command, previous code changes the current active directory to the folder to iterate.
If changing the current active directory is a problem, the file iteration can be moved to a subroutine
#echo off
setlocal enableextensions disabledelayedexpansion
set folders="..\DataSet1" "..\DataSet2" "..\DataSet3"
set extensions=".shp"
for %%f in (%folders%) do for %%x in (%extensions%) do (
call :process "%%~ff" "%%~x"
)
goto :eof
:process folderBeingProcessed extensionToProcess
for /r "%~1" %%i in ("*%~2") do (
echo Preparing table %%i
rem .....
)
goto :eof

How to copy a file with a new name with parts from original file name?

I have a bunch of files with the name 383DT_SBY_20170420_08_C.ps, 380_DB_20170421_08_C.ps, etc.
I am trying to create a script that will automatically copy and rename them to SBY_20170420_08.ps, DB_20170421_08.ps.
The following script used to work but now it tells me it can't find the file specified.
#echo off
T:
cd \PROOFS\out\
for /f "tokens=1,2,3,4,5 delims=_ " %%a in ("%1") do set first=%%a&set second=%%b&set third=%%c&set fourth=%%d&set fifth=%%e
copy %1 Renamed\"%second%%third%_%fourth%.ps"
test the existence of the file prior trying to copy it.
if you only need tokens 2 to 4, there is no need to put 1 and 5 into vars
if a (mapped) drive isn't available the seperate drive change and cd don't work, better use CD /D or Pushd and check success
the batch depends on the passed arguments if the arg doesn't match with *_*_*_*_*.ps it will fail
if the batch shall handle all files with the pattern you need an additional for loop
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
CD /D "T:\PROOFS\out\" || pause & goto :Eof
for /f "tokens=2-4 delims=_" %%a in ("%~1") do If exist "%~1" (
copy "%~1" "Renamed\%%a_%%b_%%c%~x1"
) else (
Echo %~1 doesn*t exist in %CD%
)

Copying files from one folder to another using batch

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.

Resources