Batch script: avoid infinite file loop - batch-file

Running the following batch script, I get an infinite loop even when all files are moved into the destination folder. I get the message: "The system cannot find the file specified" at each iteration.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
cd .\IMPORT
set nlot=0
:beginfor
set count=0
set /a nlot=%nlot%+1
FOR /R "." %%A IN (Zango_Sku_*.xml) DO (
IF NOT EXIST .\lot%nlot% mkdir .\lot%nlot%
move .\%%~nxA .\lot%nlot%\%%~nxA
set /a count=!count!+1
IF !count! EQU 2000 goto :beginfor
)

Related

Backup a File or Folder when dropping it on the same bat

So I've managed to cobble together a script witch works as intended for files when using "copy". It creates a backup folder "_OLD" and makes an iterated (file001, file002, etc) copy of the file I dropped on my bat by checking for the highest version in the backup folder. How ever I would like to have only one bat that would work the same way for both a file or folder. By substituting "copy" with "xcopy" I managed to copy the folder but now the file wont be copied at all. I know a little bit of programing but I'm not very experienced with bat scripts. Is there an easy fix to make it work the same on both a file and folder?
#echo off
rem set this to whatever you like
set "BackupFolderName=_OLD"
rem splits filename into name and extension etc for processing
set "OutputFolder=%CD%\%BackupFolderName%"
set "FullPath=%~1"
set "Filename=%~n1"
set "Ext=%~x1"
rem creates an output folder if none exist
if not exist "%OutputFolder%" mkdir "%OutputFolder%"
rem find the highest version of the file
set a=1
set pad=00000
:loop
rem leading zeroes
SET b=%pad%%a%
rem %var:~10% gets the sub-string from index 10 to the end of %var%
SET b=%b:~-3%
if exist "%OutputFolder%\%Filename%%b%%Ext%" set /a a+=1 && goto :loop
echo "Backed up %FilenameExt% as %Filename%%b%%Ext% in %BackupFolderName%"
xcopy /s/y "%FullPath%" "%OutputFolder%\%Filename%%b%%Ext%"
rem pause
timeout 1 >nul
exit
Here is my final code that takes multiple files or folders and does a backup of them:
#echo off
setlocal
rem set this output folder name to whatever you like eg _OLD
set "BackupFolderName=_OLD"
rem creates an output folder, if none exist
set "OutputFolder=%CD%\%BackupFolderName%"
if not exist "%OutputFolder%" mkdir "%OutputFolder%"
rem loops through all the files dropped on bat
FOR %%G IN (%*) DO (call :sub %%G)
goto :end
:sub
rem splits filename into name and extension etc for processing
set "FullPath=%~1"
set "Filename=%~n1"
set "Ext=%~x1"
rem rem find the highest version of the file
set a=1
set pad=00000
:loop
rem leading zeroes
SET b=%pad%%a%
rem %var:~10% gets the sub-string from index 10 to the end of %var%
SET b=%b:~-3%
if exist "%OutputFolder%\%Filename%%b%%Ext%" set /a a+=1 && goto :loop
rem rem checks if dropped item is file or folder
set type=invalid
for %%F in ("%~1") do (echo/%%~aF|findstr /bc:"d" >nul && set type=folder)
for %%F in ("%~1") do (echo/%%~aF|findstr /bc:"-" >nul && set type=file)
rem copies file or folder to destination
if %type%==file (copy "%FullPath%" "%OutputFolder%\%Filename%%b%%Ext%" >nul)
if %type%==folder (xcopy /s/y/q "%FullPath%" "%OutputFolder%\%Filename%%b%%Ext%\" >nul)
echo "Backed up %Filename% as %Filename%%b%%Ext% in %BackupFolderName%"
goto :eof
:end
pause
rem timeout 1 >nul
rem exit
You can check the file attributes (see for /?)
#echo off
setlocal
set type=invalid
for %%F in ("%~1") do (echo/%%~aF|findstr /bc:"d" >nul && set type=folder)
for %%F in ("%~1") do (echo/%%~aF|findstr /bc:"-" >nul && set type=file)
echo %type%

Rotate Rename Numbering files using batch script

EDITE: I have more than 100 file
I have multiple files that I want to rotate every time I run a batch.
How to do it in every SUB-FOLDER?
Here's the concept
Please help. Thanks.
#echo off
setlocal enabledelayedexpansion
REM get number of files:
set count=0
for %%a in (*.jpg) do set /A count+=1
REM rename them (decreasing by one):
for /l %%a in (1,1,%count%) do (
set /a new=%%a-1
ECHO ren %%a.jpg !new!.jpg
)
REM rename 0 to max
ECHO ren 0.jpg %count%.jpg
remove the ECHOs when you verified it's what you want.

BATCH. Using variable as folder name in path

I have problem with batch. I want to create new dir and save file to it everytime batch is opened
This is sample of my code
SET i=0
FOR /L %%i IN (0,1,100) DO (
IF NOT EXIST res\%%i (
mkdir res\%%i
GOTO run
)
)
:run
start X.exe /stext res\%i%\X.txt
Creating folders works properly. I have problem with
start X.exe /stext res\%i%\X.txt
Thanks
%i% and %%i are totally different variables. %i% is a regular batch variable, while %%i only exists within the for loop. Once you call goto run, it no longer exists.
You can store %%i in a variable and then call it using delayed expansion, because you're setting it inside of a code block (a set of parentheses).
#echo off
setlocal enabledelayedexpansion
SET i=0
FOR /L %%i IN (0,1,100) DO (
IF NOT EXIST res\%%i (
set i=%%i
mkdir res\!i!
GOTO run
)
)
:run
start X.exe /stext res\!i!\X.txt

creating thousands of folders with different names

I have to create a couple of thousand directories.
The directories look like this: 10P001 being YEAR/P,S,L,R,F/Numberfrom001to600
the letters are not systematic, so i have to write them for every folder.
my script until now looks like this, but cmd doesnt like the for loop.
#echo off
setlocal enabledelayedexpansion
set /p jahr=Welches Jahr?
set /p anzahl=Wieviele Projekte?
set looop=%anzahl+1000
for /l %%x in (1001, 1, looop) do (
set nummer=%%x
set /p welches=P1 S2 L3 R4 F5 nichtexistent6?
if %welches%==1 mkdir %jahr%P!nummer:~-3!
if %welches%==2 mkdir %jahr%S!nummer:~-3!
if %welches%==3 mkdir %jahr%L!nummer:~-3!
if %welches%==4 mkdir %jahr%R!nummer:~-3!
if %welches%==5 mkdir %jahr%F!nummer:~-3!
if %welches%==6 echo "nicht existent"
)
pause
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /f "tokens=1,2delims= " %%a IN (q28608207.txt) DO (
FOR /l %%x IN (1001,1,1011) DO (
SET seq=%%x
ECHO(MD %%a%%b!seq:~-3!
)
)
GOTO :EOF
I used a file named q28608207.txt containing this data for my testing.
10 P
10 S
The routine reads the two fields from the file as %%a and %%b, then for a loop varying %%x from 1001 by 1 to 1011 (it can of course be 1600 - 1011 just proves the point), copy %%x to a temporary veriable (because substringing can't be applied to the metavariable %%x) create a directory named %%a%%b and the last 3 characters of the temporary variable.
Note that the setlocal enabledelayedexpansion command enables the use of !var! (accessing the value of var as it varies)
The required MD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists)
To create a file containing the required numbers:
#echo off
setlocal enabledelayedexpansion
set /p jahr=Welches Jahr?
set /p anzahl=Wieviele Projekte?
set looop=%anzahl+1000
(
for /l %%x in (1001, 1, looop) do (
set nummer=%%x
echo %jahr%P!nummer:~-3!
)
)>J%jahr%.txt
This would produce a list (for jahr=14,say) in J14.txt
14P001
14P002
14P003
and then all you need do is edit the file (use notepad, not a word-processor) to replace the "P" with whatever letter is required. delete any line where no directory is required.
The resultant file could then be processed to directories by using
#ECHO OFF
SETLOCAL
FOR /f %%a IN (J14.txt) DO (
ECHO(MD "%%a"
)
GOTO :EOF
my solution for now (i know its worse than bad habit, but it works and will save me a lot of time (or at least my company some money)):
#echo off
setlocal enabledelayedexpansion
set /p jahr=Welches Jahr?
set /p anzahl=Wieviele Projekte?
set /a looop=%anzahl%+1000
set /a count=1001
:LoopStart
set /p welches=P1 S2 L3 R4 F5 nichtexistent6?
if !welches!==1 mkdir %jahr%P!count:~-3!
if !welches!==2 mkdir %jahr%S!count:~-3!
if !welches!==3 mkdir %jahr%L!count:~-3!
if !welches!==4 mkdir %jahr%R!count:~-3!
if !welches!==5 mkdir %jahr%F!count:~-3!
if !welches!==6 echo "nicht existent"
set /a count=%count%+1
if %count% leq %looop% (goto LoopStart) else (goto:eof)
pause

Batch file numbered save files

I'm using a scheduled task to run a batch file for exporting backups. Within the command for exporting it uses "FILE=filename". I gave it a name but how can I make it numbered so it doesn't overwrite? (e.g. file1, file2, file3).
Thanks
if you are in the same directory:
#echo off
setlocal enableDelayedExpansion
set "file_name_pattern=file"
for %%a in (file*) do (
set "file_name=%%~na"
)
set "last_file_number=!file_name:%file_name_pattern%=!"
echo last file number : !last_file_number!
set /a next_file_number=last_file_number
set next_file=!file_name_pattern!%next_file_number%
echo next file : %next_file%
endlocal
A solution with FOR /L
#echo off
setlocal enableDelayedExpansion
set "file_name_pattern=file"
set init=1
for /l %%n in (1;1;10000) do (
echo %%n
if not exist "!file_name_pattern!%%n" (
set last_file_number=%%n
goto :break_for
rem
)
)
:break_for
echo last file number : !last_file_number!
set /a next_file_number=last_file_number+1
set next_file=!file_name_pattern!%next_file_number%
echo next file : %next_file%
endlocal

Resources