I need to copy a file i already created (a.txt) and copy it so i will have
a1.txt
a2.txt
a3.txt
etc.
until i get a999999.txt
then, copy the files that ends with "3", for example a4453.txt to a different folder.
What is the best way to do it? vb/batch file preferred.
Thanks =]
Daniel.
#echo off
set "different_folder=c:\diff"
for /l %%n in (1;1;999999) do (
copy "a.txt" "a%%n.txt"
)
for /f %%f in ('dir /b a*.txt ^| findstr /i /e "3.txt"') do (
copy "%%~ff" %different_folder%
)
Not tested.
Related
I would like to rename directories whose name contains the word "NOTE", but not "NOTES", to "NOTES". I first experiment with the echo command.
for /f "tokens=1-7" %%i in ('dir d:\mydirectory /s /b /ad ^|find "NOTE" ^|find "NOTES" /v') do #echo %%i %%j %%k %%l %%m %%n %%oS
Because directory names have different spaces in them, the above command may leave spaces between "NOTE" and S. Anyway to overcome this problem?
Give this a burl. If it echo's the right rename command then remove the echo and the pause to activate it. It's untested. Paths containing ! and % characters will cause an issue.
#echo off
setlocal enabledelayedexpansion
for /d /r %%a in (*) do (
set "f=%%~nxa"
if not "!f:NOTE=!"=="%%~nxa" (
if "!f:NOTES=!"=="%%~nxa" (
echo ren "%%a" "!f:NOTE=NOTES!"
pause
)
)
)
I have a batch file that checks a root directory every 10 seconds for PDF files and copies those files to their corresponding subfolders of the exact same name.
What I'm missing is that I need my batch file to check the target subdirectory for the named file, and if the named file exists, to rename the new file to be copied to !filename!001.pdf and have the 001 get incremented as duplicate file names are copied. Here's what I got:
:loop
setlocal enabledelayedexpansion
cls
pushd c:\files\
for /f "tokens=*" %%1 in ('dir /a-d /b c:\files\*.pdf') do (
set filename=%%~n1&set dirname=!filename:~0,7!
dir c:\files /b *.pdf > location1list.tmp
for /f %%a in (location1list.tmp) do dir c:\files\%%a > location2list.tmp
if not exist c:\files\!dirname! (md c:\files\!dirname!)
move %%1 c:\files\!dirname!\>nul
)
timeout /t 10
goto:loop
Any suggestions how I can get the files renamed?
I.e. if Bob.pdf exists in the Bob folder and another Bob.pdf is added to the C:\files folder, I want it to be copied to the C:\files\bob folder as Bob001.pdf.
Something like this should do what you want. May need some tweaking. Also I have not tested it, just wrote it from memory. Need any explanations, just ask.
Script
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd C:\files
:Main
for /f "tokens=*" %%A in ('dir /a-d /b *.pdf') do (
if not exist "%%~dpnA\*" md "%%~nA"
set "File=%%~dpnA\%%~nxA"
if exist "!File!" call :Name "!File!" File
echo !File!
move "%%~fA" "!File!"
)
goto Wait
:Name <Target> <Variable>
set "Count=0"
:Count
set /a "Count+=1"
set "Number=00%Count%"
if exist "%~dpn1%Number:~-3%%~x1" goto Count
set "%~2=%~dpn1%Number:~-3%%~x1"
goto :eof
:Wait
timeout /t 10
goto Main
:End
popd
endlocal
Limitations
Each target subdirectory can only handle a maximum of 1000 files as this is written. If the subdirectory hits this maximum then the script will get stuck in an infinite loop.
Update
Fixed an error in my initial script
I have the next script, and I need it to save all the xcopy files copy outputs to one log file,
:tmdeploy
title Deploying Edithor - %deployer%
set src_folder=S:\ApliTelinver\Compilacion\Edithor 10.5\CompilacionQA
set dst_folder=S:\ApliTelinver\Ambientes-Edithor\Sincronizacion\Test\Test-Mantenimiento
set filelist=filelist-tm.txt
echo Origen: %src_folder%
echo Destino: %dst_folder%
echo.
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
)
echo.
pause
goto end
The problem is that I only get the last file copy in the output. And how to properly do a timestamp for .log file?
Thank you
You should use the appended redirection operator, >> instead of >.
So, your for loop will look like this:
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
)
I'm having space issues on my Vista machine and need to figure out what's taking up so much space.
I would like to write a simple batch file that returns all folders under C: and the size of each folder.
The dir command doesn't appear to return folder size.
Unfortunately we don't have admin rights and can't install a third party application and we have other users in our group that also need this information.
I'd have a look at this thread for some clues as to how to achieve the directory size:
Batch File To Display Directory Size
Otherwise:
dirsize:
#echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
#echo %CD%:!sum! k
AllDirSize:
echo off
set WORKING_DIRECTORY=%cd%
for /f "delims=" %%a in ('dir /a:D /D /B /S') do (
echo off
cd %%a
"%WORKING_DIRECTORY%"\dirsize "%%a"
cd %WORKING_DIRECTORY%
)
Use it: ALLDIRSIZE > C:\temp\FileContainingFolderSizes.txt
Which is taken from the excellent Richard Bishop testing forums: http://www.bish.co.uk/forum/index.php?topic=58.0
Not exactly answering your question, but if you have GUI access I'd suggest using TreeSize:
http://www.jam-software.com/freeware/index.shtml
If you prefer command line use du command from Unix utils:
http://unxutils.sourceforge.net/
I have a windows batch file that does this:
for %%s in (*.sql) do call
It loops through all the sql script in a folder.
In the folder the file names are like:
s4.06.01.sql
s4.07.01.sql
s4.08.01.sql
s4.10.01.sql
s5.01.sql
But the for loop goes through the files randomly (not in the name order), it first run s5.01, then s4.06, then s4.08, then s4.10, then s4.07. How can I make them run in the name order?
It used to work, but now it doesn't. What may cause this problem?
Jerry's answer might very well be what is causing the problem.
You might solve it by changing
for %%s in (*.sql) do call
to
for %%s in (dir"*.sql ^| sort) do call
Edit cudo's to LonelyPixel
The posted solution does not work as is on a Windows 8 machine (perhaps it did back then with Windows XP but I have no idea).
Following is a working solution using Windows 8 command prompt.
for /f "tokens=*" %%s in ('dir /b *.sql ^| sort') do echo %%s
If memory serves, it will operate on the files in the order they're returned by the file system. As such, if you run it on a disk partition that's formatted with NTFS, the names will be returned in sorted order so they'll be processed in sorted order. If the disk partition is formatted with something like FAT or FAT32, the names will be retrieved (and processed) in a more or less random order.
I too had to deal with spaces in the filenames, so I added quotes around the input variable like so:
for /f "tokens=*" %%s in ('dir /b *.sql ^| sort') do echo "%%s"
In my case it was SQLCMD, with the scripts in a subdirectory, like so:
SET "machineName=localhost"
for /f "tokens=*" %%f in ('dir /b PostDeployScripts\*.sql ^| sort') do sqlcmd -E -S%machineName% -i"PostDeployScripts\%%f"