How to duplicate one directory/folder and its contents 100 times? - batch-file

I need to know how to make 100 copies of the same folder and its sub-folders.
I know how to copy once with the following:
for /l %i in (1,1,1) do xcopy /i /e "folder 1" "folder 2"
Is there a way to make this code run 100 times and make 100 copies of folder 1 and its contents?

Just try to increase the %L variable in your for /L loop, from 1,1,1 to 1,1,100...
In command line:
for /L %L in (1,1,100)do %__APPDIR__%xcopy.exe /i /e "folder 1" "folder %L"
In bat/cmd file: :
#echo off && for /L %%L in (1,1,100)do %__APPDIR__%xcopy.exe /i /e "folder 1" "folder %%L"
your cmd: ( 1, 1, 1 ) == 1, 1, 1
loop: ( start, increase, until ) == x,+1, y
you need: ( 1, 1, 100 ) == 1, 1, 100
Take a time to read: For /?

Related

Why is the letter D from /D option of command FOR missing in the output?

I'm creating a batch file to delete all files and folders except for TEMP folder. I noticed the letter D of the option /D is missing in the output. The script seems to work fine, but I wonder why it's missing.
#echo on
#Y:
REM Following dangerous command commented out for safety - magoo
REM for %%f in (*) do del "%%f"
for /D %%d in (*) do (
if /I not "%%d" == "TEMP" echo "%%d"
)
pause
Output:
Y:\>for %f in (*) do del "%f"
Y:\>for / %d in (*) do (if /I not "%d" == "TEMP" echo "%d" )
Y:\>(if /I not "TEMP" == "TEMP" echo "TEMP" )
Y:\>(if /I not "test" == "TEMP" echo "test" )
"test"
Y:\>(if /I not "test 2" == "TEMP" echo "test 2" )
"test 2"
Y:\>pause
I'm echoing just for test purpose. I will replace it finally with rd /S /Q.

Batch nested loop doesn't work properly with errorlevel

I have a problem with a nested loop and usage of errorlevel. I want to delete files on multiple computers with batch files. For this I have a batch file that iterates through the computers and calls another batch files that deletes the files. On some computers I want to delete the files in a second folder.
The first part of the code works as expected, but the second part with the nested loop doesn't work. the code is:
SETLOCAL enabledelayedexpansion
REM %1 is a parameter from the calling batch file
FOR %%A IN (M:\Folder\) DO del /Q "%%A" 2>&1 1> NUL | find /V "" 1> NUL 2>&1 & IF ERRORLEVEL 1 (1> NUL ver) ELSE (2> NUL SET =)
IF !ERRORLEVEL! == 1 (
ECHO !ERRORLEVEL!
) ELSE (
ECHO OK
)
FOR %%N IN (%computers%) do (
IF %%N EQU %1 (
FOR %%A IN (M:\Folder2\) DO del /Q "%%A" 2>&1 1> NUL | find /V "" 1> NUL 2>&1 & IF ERRORLEVEL 1 (1> NUL ver) ELSE (2> NUL SET =)
IF !ERRORLEVEL! == 1 (
ECHO !ERRORLEVEL!
) ELSE (
ECHO OK
)
)
)

Print single lines, and a number of lines, with the contents i text file. Batch skript

I would like to print single lines,or a number of lines, with the contents, between the desired line numbers (from the n to m lines numbers), in text file!
I have trouble printing lines, with the contents, under number lines 10, both single lines (eg, only 1, or 4, etc.), or several desired lines (eg from 1 to 9 or from 3 to 13 etc.) in the text file. To print liner, with contents, over number lines 10, both single lines (eg, only 11, or 16, etc.) and the desired number of lines (eg 10 to 19 or 14 to 16, etc.) works well. Where I'm wrong?
CODE:
#echo off
for /f "tokens=1* delims=:" %%a in ('findstr /n .* "data.txt"') do (
if "%%a" EQU "%1" echo.%%b else (if "%%a" GTR "%1" if "%%a" LSS "%2" echo.%%b))
::>>temp.txt
if "%%a" EQU "%1" echo.%%b else (if "%%a" GTR "%1" if "%%a" LSS "%2" echo.%%b))
should be
if "%%a" EQU "%1" (echo.%%b) else (if "%%a" GTR "%1" if "%%a" LSS "%2" echo.%%b))
as batch needs to differentiate between echo %%b and echo %%b else ... being alternative strings to echo.
Perhaps you should also see This earlier example which makes me feel that this is homework...

Batch File returning folder size and delete content if greater than 50gb

I am trying to write a batch script, which will first return the sum of multiple folder locations and if these are greater than 50GB, delete the content.
I was googling around and still don't know how to return the size of a folder (in GB).
Can anyone help`?
The size of a folder can be derived by dir /S (see the summary at the very bottom of the output, which contains the total amount of bytes occupied by all sub-items). This can be extracted like this:
set "PREVB="
for /F "skip=2 tokens=3" %%B in ('
dir /S /-C "\path\to\folder"
') do (
call set "BYTES=%%PREVB%%"
set "PREVB=%%B"
)
But be careful: do not treat this number as an integer (by set /A or if EQU/GTR/...), because cmd supports signed 32-bit integers only, which is easily exceeded by folder sizes.
To check against 50 GB you might split the resulting number, like:
rem // Split huge number into GB and ones:
set "BYTES_GIGA=%BYTES:~,-9%"
set "BYTES_ONES=%BYTES:~-9%"
rem // Ensure GB part to be non-empty:
if not defined BYTES_GIGA set "BYTES_GIGA=0"
rem // Remove leading zeroes from ones:
set /A "BYTES_ONES=1%BYTES_ONES%%%1000000000"
Then you can round the GB number up, if you wish:
if %BYTES_ONES% GTR 0 set /A "BYTES_GIGA+=1"
Finally, check the GB number whether or not it exceeds the predefined limit:
if %BYTES_GIGA% GTR 50 (
rem // Do something, perhaps delete the entire folder:
rmdir /S /Q "\path\to\folder"
)
Seems to me that dir /s /-c returns a final block:
Total Files Listed:
51 File(s) 88492332 bytes
14 Dir(s) 112224022528 bytes free
And you just need to parse out that value (88492332).
Can you provide more explicit detail about what code you have, and why it isn't working?
This works for me:
GetSize.bat
for /F "delims=:" %%a in ('dir /-c /s ^| findstr /N /C:"Total Files Listed"') do set SKIP=%%a
for /F "tokens=3" %%a in ('dir /-c /s ^| more +%SKIP%') do (set FOLDER_SIZE=%%a & goto :end)
:end
echo Final Folder Size is %FOLDER_SIZE%
with robocoby Output and erase
The code was reworked.
#echo off
setlocal
prompt $g$s
rem set size= 150.0 m
rem usage -> see SUB :Fsize
set size= 50.0 g
call :createTMP
set /a _N=0
:Listing
rem list Folder List size and delete
for /D %%F in (
D:\1?
D:\80
D:\2014-*
D:\a
) do (
if exist %%F if /i NOT "%~0"==":Listing" (
set /a _N+=1
call mklink /j "%%empty.link.tmp%%\%%_N%%" "%%~F"
) else call :delall "%%~F"
)
rem --- END SETTINGS ---
if :SUB==only (
:createTMP
set "empty.tmp=%temp%\empty_%time::=%"
set "empty.link.tmp=%temp%\link_%time::=%"
md "%empty.tmp%"
md "%empty.link.tmp%"
exit /b
)
if /i "%~0"==":Listing" (
:deltmp
for /d %%D in (
"%empty.link.tmp%\*"
"%empty.link.tmp%"
"%empty.tmp%"
) do rd "%%~D"
exit /b
)
:Listsize
call :Fsize "%size%" size.max
for /f "skip=2tokens=6*" %%a in ('
robocopy /L /purge /njh "%empty.tmp%" "%empty.link.tmp%" /nfl /ndl ^|find " 0 "
') do call :Fsize "%%b" size.out deci.mal pre
if NOT %size.out% lss %size.max% (
echo Folders: %deci.mal% %pre% ^> %size%
call :Listing
)else echo Folders %deci.mal% %pre% ^< %size% &call :deltmp
exit/b
:Fsize "512.0 k" [VAR1-floating point [VAR2-to display only [VAR3-prefix]]]
rem to use with output of robocopy
rem set with decimal minimum 1/1 max b =1024
rem set with decimal minimum 1/10 max k =.9 rounded + 102.3 bytes (+ 10 %)
rem decimal minimum 1/100 max m =.99 rounded + 10.23 KiB (+ 1 %)
rem decimal minimum 1/1000 max g =.999 rounded + 1.023 MiB (+ 0,1 %)
rem 500.0 b -> 500 bytes
rem 200.5 k -> 200,5 KiB
rem 350.04 m -> 350,04 MiB
rem 1.001 g -> 1,001 GiB
rem VAR1 size.out - Binary prefix and number to floating point
rem VAR2 deci.mal - Reverse the calculation part to display only
rem VAR3 pre.fix - Reverse the calculation to add prefix
set/a p=-1,TC=10000
for %%N in (b k m g t p)Do set /a "p+=1,%%N=p"
for /f "tokens=1-3delims=. " %%A in ("%~1 0 b")Do (
set/a"pre.fix=TC*(%%C+1) + %%A"
set "deci.mal=%%A.%%B"
setlocal enabledelayedexpansion
set "FS=!pre.fix!.%%B"
for /f "tokens=1,2" %%S in ("!FS! !deci.mal!")Do (
endlocal
if NOT "%~2"=="" (set "%~2=%%S")else set "size.out=%%S"
if NOT "%~3"=="" set "%~3=%%T"
)
)
set/ax=pre.fix/TC-1
for %%s in (b k m g t p)Do 2>nul set/a"N/=(%%s-x)"||^
if NOT "%~4"=="" (set "%~4=%%s")else set "pre.fix=%%s"
exit /b
:delall
rem be carfully - this will erase all Data !
rem 1st with option /L only list this Files
rem without /L will erase all Data
:#echo off
setlocal
set "T=%temp%\DF%time::=%"
md "%T%" ||exit /b 1
:Delete
if "%~1" equ "" goto :end
robocopy /L /fp /purge /ns /np /njh /njs "%T%" "%~dp1\" "%~nx1"
shift
goto :Delete
:end
rd "%T%"
exit /b 0
robocopy size batch-file del

move tiff/tif files having a hyphen in the name

I have a batch file which moves .tiff/tif files from one folder to another if the filename has digits, for example,
0000002341567.tif. It is working fine but
my requirement is to move file even if it has a name like 000000234156-7 or 0000002341567-s
So to say the file name can be suffixed with - and a one digit number or a hyphen and a character.
for %%I in ("C:\Documents\Pictures\*.tif*") do (
if !FileCount! EQU 0 (
echo Exiting after having moved already %FileCount% TIF files.
goto LoopEnd
)
set "HasOnlyDigits=1"
for /F "tokens=1 delims=0123456789" %%T in ("%%~nI") do set "HasOnlyDigits=%%T"
if "!HasOnlyDigits!" == "1" (
move /Y "%%I" "%FolderGood%"
)
findstr has a very limited set of REGEX, but it is enough for this task:
#echo off
set filecount=1
setlocal enabledelayedexpansion
for %%I in ("*.tif*") do (
if !FileCount! EQU 0 (
echo Exiting after having moved already %FileCount% TIF files.
goto LoopEnd
)
echo %%~nI|findstr /R "^[0-9][0-9]*-.$" >nul && (
echo yes %%I
ECHO move /Y "%%I" "%FolderGood%"
set filecount+=1
) || (
echo no %%I
)
)
:loopend
The REGEX here consists of:
^ Start of the line (string)
[0-9] Any digit
[0-9]* any digit (zero or any nunmber of any digit) (previous [0-9] to ensure, there is minimum one digit)
- a dash
. any character
$ End of the line (string)

Resources