.bat file to xcopy files of greater than certain size - batch-file

I have this code in a .bat file which works ok, except I want to only copy files greater than 50 KB.
cd "C:\Users\Public\Documents\NTLogFiles"
Xcopy "C:\Users\%USERNAME%\Documents\NinjaTrader 8\log\log.????????.0000?.en.txt" "C:\Users\Public\Documents\NTLogFiles\log.????????.txt" /A /D /Y
I'm trying to work with this, which is not getting it done.
cd "C:\Users\Public\Documents\NTLogFiles"
for %%a in (log*.txt) do if %%za geo 50000 (Xcopy /p /a /d /y %%~za "C:\Users\Public\Documents\NTLogFiles\log.????????.txt")
I finally settled on using robocopy to get the files I wanted by size and then using xcopy to move them to their final destination while getting them renamed.
ROBOCOPY "C:\Users\%USERNAME%\Documents\NinjaTrader 8\log" "C:\Users\Public\Documents\NTLogFilesTemp" log.*.en.txt /Min:100000
Xcopy "C:\Users\Public\Documents\NTLogFilesTemp\log.????????.0000?.en.txt" "C:\Users\Public\Documents\NTLogFiles\log.????????.txt" /A /D /Y

Related

batch xcopy only files larger than a certain size

Have to extract from a recovered 4TB HDD some files
to extract jpg files I'm using the following script:
#echo off
cd /d "f:\old"
for /d %%a in (*.*) do xcopy /y "%%a\*.jpg" "F:\jpg\"
but.. if I would copy only files larger than 100 KB.. what shall I add to the script?
sorry if question is too easy but my knowledge of batch script is practically zero.
Thanks
Joe
Try this:
#echo off
pushd "f:\old"
for /d %%a in (*.*) do (if %%~zF geq 102400 (xcopy /y "%%F" "F:\jpg\"))
This will copy all files which are "Greater than or equal (>=)" 100KB.
This should work:
#echo off
cd /d "f:\old"
for /d %%a in (*.*) do if %%~za geq 102400 (xcopy /y "%%a\*.jpg" "F:\jpg\")
We can get size of the file by %%~z{your_variable}

Create batch file to copy entire directory to new variable directory

Here's my problem. Playing a PC game (Arkham City) that can sometimes corrupt the save out of nowhere. My idea is to copy the save data to a new directory (as opposed to overwriting it) every time I start the game. In fact, this batch file will do everything. Copy files to new directory, and execute the game. I want it to copy all of the files to a variable directory every time I execute the batch into a new folder like Backup1, Backup2, etc.
There are two directories this games uses for save data - need them both:
C:\Users\Daddy\Documents\WB Games
C:\Users\Daddy\AppData\Local\Microsoft\XLive
Those directories and subdirectories when then copy to a location like this:
C:\Users\Daddy\Documents\Batman\GameSaveData\Backup1\ where the value after 1 is the variable. So literally every time I execute this batch, it dumps a new set of save data into a new Backup% folder at that directory.
The example below is static, but just overwrites to same directory. Unfortunately I wouldn't know if my game was corrupted until I go to play, but by then, the batch will have overwritten the good save with the corrupted save. This is why I want to always dump into a new backup folder.
xcopy "C:\Users\Daddy\Documents\WB Games" "C:\Users\Daddy\Documents\Batman\GameSaveData\WB Games" /D /E /C /R /I /K /Y /S
xcopy "C:\Users\Daddy\AppData\Local\Microsoft\XLive" "C:\Users\Daddy\Documents\Batman\GameSaveData\XLive" /D /E /C /R /I /K /Y /S
"C:\Program Files (x86)\WB Games\Batman Arkham City GOTY\Binaries\Win32\BmLauncher.exe"
I'd suggest you substring %date% and %time% to a yyyymmddhhmmss variable and use that in place of 1,2 etc.
Quite how you'd do that would depend on your date and time formats, but it's documented extensively on SO.
Using the yyyymmddhhmmss format will mean that the last directory in a dir list would be the last chronologically...
This should do the trick:
(will create a subfolder in "%Userprofile%\Documents\Batman\GameSaveData" named YYYY-MM-DD_HourMinuteSecond and copy the contents to it)
#ECHO OFF
FOR /F "SKIP=1 TOKENS=1-7" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF NOT "%%~F" == "" (
SET YYYY=%%F
SET MM=00%%D
SET DD=00%%A
SET HH=00%%B
SET Min=00%%C
SET Sec=00%%E
)
)
SET friendlyTime=%YYYY%-%MM:~-2%-%DD:~-2%_%HH:~-2%%Min:~-2%%Sec:~-2%
SETLOCAL ENABLEDELAYEDEXPANSION
IF NOT EXIST "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%" MD "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%"
ENDLOCAL
XCOPY "%Userprofile%\Documents\WB Games" "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%\WB Games" /D /E /C /R /I /K /Y /S
XCOPY "%LocalAppData%\Microsoft\XLive" "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%\XLive" /D /E /C /R /I /K /Y /S
Edit:
i haven't checked your XCOPY-Commandline, just copy and pasted it. :)

XCOPY overwrite action?

in the bat file I got a simply command like :
xcopy "C:\Users\me\Documents\ApplyPatchBat" "C:\Users\me\Desktop\Test\" /S/E/K
This does the job where it will copy all the files including files inside subfolders. I know you can add /y to make it auto overwrite.
How ever I want to create an statement if this file that we are copying exist in the destination folder, copy the one in destination folder to a backup folder.
I wonder there is any IF statement I can add to after the command to detect if the file is existing? Or maybe an error level check when its asking do I want to overwrite? P.S. This is for applying new patches to a program, where there are many layers of folders, that's why I use the /S/E/K in xcopy.
xcopy itself can't backup beforehand, but with output from xcopy and a bit of for loop magic you can make this happen
#echo off
setlocal ENABLEDELAYEDEXPANSION
set source="C:\Users\me\Documents\ApplyPatchBat"
set target="C:\Users\me\Desktop\Test\"
rem /l = list only, /u = already existing, /y = yes to replace
rem /f = display full source and destination file names, /c = continue on errors
rem /s = subdirectories, /k = keep attributes
rem split the strings at ->, we're only interested in the part after ->
for /f "usebackq tokens=1,* delims=>" %%a in (`xcopy %source% %target% /l /u /y /f /c /s /k`) do (
set file=%%b
rem ignore lines without a destination, e.g. 15 file(s) copied
if x!file! neq x (
rem remove the leading space, original string is source -> destination
set file=!file:~1!
for %%f in ("!file!") do (
if not exist %%~dpf\backup\* md %%~dpf\backup
rem only backup if not already backed up
if not exist %%~dpf\backup\%%~nxf move %%f %%~dpf\backup
)
)
)
xcopy %source% %target% /y /c /q /s /k

I want to make a batch file that copies *.inf files in hard disc to my pen drive on autorun?

Today I was trying to create a batch file which up on execution, searches my d drive for inf files and copies them to my pen-drive files folder. I was trying this code:
for /r "d:\" %i in (*.inf) do copy /y "%~fi" "\files"
How to put it in a batch file and automate it...
Double the percent characters %% on for loop variables when in a batch script.
for /r "d:\" %%i in (*.inf) do copy /y "%%~fi" "\files"
Recommended for speed:
xcopy D:\*.inf "\files" /S /I /Y
Note The source and target drive cannot be the same for the xcopy method.
Or
for /f "delims=" %%A in ('dir /a-d/b/s D:\*.inf') do copy /y "%%~fA" "\files"

How to copy files from a random folder into a destination folder in batch?

I need a batch script to copy files from a random subfolder of a specific directory into a destination directory.
For example, there will be a number of files in their own subdirectory, for example
.../source/a/file.txt
.../source/b/file.txt
So there a number of these files, and I would like to randomly select one of them and have it copied into the new directory
.../destination/file.txt
So the file.txt in the destination is just being overwritten with other files that have the same name but different content.
I'm new to batch scripting and I can't quite figure out how to select each file from a random subfolder. I'd also like it to repeat every 30 seconds until I terminate the script, but I think it should be easy enough to just make a second script that calls this .bat file every 30 seconds once I get it going.
Thanks!
This can do what you request. Just set your source directory, destination directory, and your file name filter.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd "...\source\"
:: Enumerate Files. Method 1
set "xCount=0"
for /r %%A in (file.txt) do if exist "%%~A" set /a "xCount+=1"
echo %xCount%
:: Select a Random File.
set /a "xIndex=%Random% %% %xCount%"
echo %xIndex%
:: Find an Copy that File. Method 1
set "xTally=0"
for /r %%A in (file.txt) do if exist "%%~A" (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
:End
popd
endlocal
pause
Type xcopy /? to see all of its options.
Here are some alternate loop methodologies for the file enumeration.
:: Enumerate Files. Method 2
set "xCount=0"
for /f %%A in ('dir *.txt /a:-d /s ^| find "File(s)"') do set "xCount=%%~A"
echo %xCount%
:: Find an Copy that File. Method 2
set "xTally=0"
for /f "delims=" %%A in ('dir *.txt /a:-d /b /s') do (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
Enjoy :)
#echo off
setlocal EnableDelayedExpansion
rem Enter into the directory that contain the folders
pushd \Fullpath\source
rem Create an array with all folders
set i=0
for /D %%a in (*) do (
set /A i+=1
set folder[!i!]=%%a
)
rem Randomly select one folder
set /A index=(%random%*i)/32768 + 1
rem Copy the desired file
copy "!folder[%index%]!\file.txt" "\Fullpath\destination" /Y
rem And return to original directory
popd
The features of batch script is
It copies all files from source to destination folder with similar structure(even retains empty folders).
Can retain N number of days recent files in Archive folder(source) remaining files will be moved to backup folder(destination).
Can be scheduled to N number of days to N number of years.
Can be used in any Source to destination folder backup.
Source folder can add N number of folder any time and delete folder or files, but Destination folder always adds the folder and never deletes any folder or file.
#echo off
Set "sourcefolder=E:\Interfaces"
Set "destinationfolder=E:\BackupInterface"
If Exist %sourcefolder% (
For /F %%* In ('Dir /b /aD "%sourcefolder%" 2^>nul') do (If Not Exist "%destinationfolder%\%%*" ( RD /S /Q "%destinationfolder%\%%*")
xcopy /e /v /i /y /q "%sourcefolder%\%%*" "%destinationfolder%\%%*"
forfiles /p "%sourcefolder%\%%*" /s /d -30 /c "cmd /c del /Q /S #file" )
) Else (echo.Source folder could not be found)
:end of batch
echo.&echo.finished!

Resources