There is a folder which contains many zip files. I want to copy only oldest zip file on each commmand execution. Please help? Thanks
From How do I write a Windows batch script to copy the newest file from a directory? - but reversing the file order
FOR /F "delims=" %%I IN ('DIR . /B /O:D') DO COPY %%I NewDirectoryPath & EXIT
Related
I want to, for example, replace all -v1 parts in every filename for -v2. It's almost working, but the access is denied because the files are being used by the cmd process itself during execution. My current code is:
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do ren "%%a" "%%a:-v1=-v2"
Any suggestions? :)
EDIT:
I've also tried for /r %%i in ("*-v1*") do ren "%%~nxi" "%%~nxi:-v1=-v2" and it finds the files and uses the correct rename value, but still outputs that it can't access the file because it is in use by another process. I'm sure that process is the cmd.exe itself, because after I close the command prompt, I can change the filenames manually without any problems.
I also tried by writing the current filenames to a temporary .txt file with the idea that the files I want to rename aren't used by any command. Then read the file with the type command within a for loop to rename each file, but same thing.
It's quite frustrating, any help is appreciated :)
substring substituion doesn't work with for variables (%%a). Use a "normal" variable and delayed expansion:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do (
set filename=%%a
ren "%%a" "!filename:-v1=-v2!"
)
I would like to robocopy a directory and it's subdirectories to another directory. If a file at source is newer then I would like to make a copy of this file by adding a date/time stamp at end of the filename at destination and do the copy to destination.
I do not see any switches in the robocopy to do this. Can someone guide me how to do this.
Robocopy doesn't have a renaming switch, but you can use the rename command on the resulting files to add timestamps. Here's an example batch file:
#echo off
for /f "tokens=1-3 delims=. " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
for /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
dir Directory1\ /b > list
robocopy Directory1\ Backup\
for /f %%f in (list) do rename Backup\%%f %%~nf%mydate%_%mytime%%%~xf
Note that you will need to change the delimiters of the date depending on which country standard you follow. You can get that by executing date /t
How can I write in a simple Windows 7 compatible Batch file:
Where filename begins with "c:\my folder\myfile*.exe", run only the most recent one created.
For example, if I have 10 files in "c:\my folder\" and they are all similarly named myfile*.exe, and myfileBOB.exe was the last of this named file to be created - how to fish this out (the folder also contains other general files of different types) automatically by filename myfile* AND created date to execute?
Many thanks!
Sort the files by date ascending, and keep the last (most recent) one.
#echo off
setlocal
pushd "c:\my folder"
set "file="
for /f "eol=: delims=" %%F in ('dir /b /a-d /od myfile*.exe') do set "file=%%F"
if defined file "%file%"
popd
Or sort files by date descending, and break out of loop after first iteration.
#echo off
pushd "c:\my folder"
for /f "eol=: delims=" %%F in ('dir /b /a-d /o-d myfile*.exe') do "%%F"&goto :break
:break
popd
I'm trying to figure this out need some help. I need to make a program that copy's my Log file and rename with date and time to different directory. Prior to loading the program not sure how to do it if any help would be great.
Here's a batch file that will copy all log files to an archive folder appending the date to each:
#Echo Off
#For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do #(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
#For %%a in ("*.log") do copy %%a "archive\%%~na_%All%.LOG"
I have tens of thousands of *.wav files spread out across hundreds of folders. I need help to get command line to move all files up one level. The directory structure for all these files are identical, but the names of the folders vary slightly:
Z:\Audio\Level*\story*\VOCAB\*.wav
All files are located in the VOCAB folders, and I need to move them to the story* folders:
Z:\Audio\Level*\story*\*.wav
I can do this from command line by running a move command on each individual folder, but is there a way to run it recursively on all files within the entire directory? Can I use a wildcard in the location path?
Notes:
The * in Level* and story* are numbers 01-24.
I'm on Windows XP Professional.
Thanks for any help you can provide!
try something like:
for /r %F in (*.wav) do move %F %~pF\..
refer to for /? from command prompt as reference (particularly in case I didn't 'code' that quite right...)
I suggest starting it from within \Audio directory.
Found a similar question in another forum (http://www.computerhope.com/forum/index.php/topic,98046.0/all.html).
Modifying some of their code, here's a batch file script that might do the trick (please try on a small subset before unleashing it on everything):
#echo off
set thisdir=%cd%
for /f "delims=" %%A in ('dir /b /ad') do (
cd /d "%%~dpnA"
for /f "delims=" %%B in ('dir /b /ad') do (
echo Level 2 Directory: %%~dpnB
cd /d "%%~dpnB"
for /f "delims=" %%C in ('dir /b /ad') do (
echo Level 3 Directory: %%~dpnC
cd /d "%%~dpnC"
move *.* ..\
cd ..
rd "%%~dpnC"
)
cd ..
)
cd..
)
cd /d "%thisdir%