I have folders that have the following structure:
/folder/processed_2013_0101
/folder/processed_2013_0223
etc.
What I'm wanting to do, is write a batchscript to move all of the files out of the most recent processed folder.
How would I go about specifying which folder that is?
#ECHO OFF
SETLOCAL
FOR /f %%i IN ('dir /b /ad \folder\processed_*') DO SET mrf=%%i
ECHO Most recent=%mrf%
Then move %mrf%\* destination
wherever destination is...
Related
I have a movie collection setup on my nas and am trying to rename all of my subtitle files to the same name as the movie located in the same directory. For example:
D:\Movies\2 Guns (2013) (Bluray)\
-2 Guns (2013).mkv
-English.srt
I would like for the english.srt file to be renamed to:
2 Guns (2013).srt
Long story short, I can't use any type of media manager to accomplish this. I would love for it to be a batch file, but am willing to use powershell or even a 3rd party program as long as it isn't a media manager of some sort. I need the name to be generated based off the largest file located in the same directory, not the folder name as some of these do not match the filenames. Also, the .srt files will not always be named english.srt
EDIT-
So I came up with this:
#echo off
setlocal enabledelayedexpansion
for /F "delims=" %%A in ('dir /b *.mp4 *.avi *.mkv') do (
set basename="%%~nA"
ren *.srt !basename!.eng.srt
)
This works beautifully, but how can I make this recursive so I can run it from parent directory vs one by one?
Just change dir /b ... to dir /b /s ... this will recurse through sub directories.
You may need to include full paths where you do the renaming too.
Here's the whole script.
setlocal enabledelayedexpansion
for /F "delims=" %%A in ('dir /b /s *.mp4 *.avi *.mkv') do (
set basename="%%~nA"
ren "%%~dpA*.srt" "!basename!.eng.srt"
)
Note I made some changes to the ren command too.
We wrap the name with quotes incase you have spaces to deal with. And we also use %%dpA to clarify which drive and path we want to work from.
I am trying to create a .cmd script to archive my project folders in SharePoint using robocopy. The issue that I am having is that there are many folders in the source dir that I do not want to archive.
#set sourcepath=\\foo\bar\p_*
I prepended my project folders with 'p_' so I could set my source variable to /foo/bar/p_*. This way I would pick up each folder and all of it's files without grabbing all of the other 'non-project' folders. Obviously this did not work.
Is there a way to pull this off without having to build a folder list that I need to manage every time a project folder is added?
Your input is appreciated
You can get the list of directories with a command.
dir /A:D /B /r p_*
Then you can use a FOR loop to start robocopy for each directory.
SETLOCAL
SET SOURCEDIR=\\source\...
SET DESTINATIONDIR=\\dest\...
CD %SOURCEDIR%
FOR /F %%G IN ('dir /A:D /B /r p_*') DO robocopy "%SOURCEDIR%\%%G" "%DESTINATIONDIR%\%%G" /e
The below lines need to copy all the folders from \Reports\Input to
\Deliver\Unidentified\Requests. Input directory contains lot of folders which contains files.
but Below code is not copying.
If I change dir /b /ad *`) then it is copying only the files from the input folder subdirectories files.
How to change this Please help on this. Thanks advance
Struture:
under Report --> Deliver,Input folder is there
under Input folder -- Response 1,2,3... folder which contains files.
CD %AUTOMATEDTESTHTTPDIR%\..\Reports\Input
FOR /F "usebackq tokens=*" %%i IN (`dir /ad *`) DO (
ECHO %%i
CD "%%i"
COPY * ..\..\Deliver\Unidentified\Requests
cd..
)
xcopy /S %AUTOMATEDTESTHTTPDIR%\..\Reports\Input\* %AUTOMATEDTESTHTTPDIR%\..\Deliver\Unidentified\Request\
should work. The /S parameter is for recursively copying the files.
You might not need to use a for-loop at all.
I need to copy a file and paste it in many directories, can I do this with a single command in windows prompt?
I tried this code, but it didn't work:
> copy C:\main\folder-1\docs\file.txt C:\main\*\docs
The names are illustratives, but the idea is: inside the "main" folder I have 50 folders ("folder-1", "folder-2", ..., "folder-50")... and inside each "folder-N", I have other folder named "docs". Every time that I create a file into any "folder-N\docs" I need to paste it into all "folder-N\docs".
Is it possible? or I really need to paste the file, folder by folder?
Straight up from the command line:
for /D %x in (c:\main\*.*) DO COPY c:\main\folder-1\docs\file.txt %x\docs\file.txt
From a BAT file or CMD file (not from the command line), you need to escape the % variable again
for /D %%x in (c:\main\*.*) DO COPY c:\main\folder-1\docs\file.txt %%x\docs\file.txt
Of course, if the subdirectory "docs" directory doesn't exist in each subfolder of "main", the iteration will print an error. That's why in my example above I explicitly specify copying to %x\docs\file.txt. If I had just said `%x\docs" as the target of the copy, it might create a file called "docs" that contains the contents of the file.txt source.
More information on for loops here and here:
Or just type "help for" at the command prompt.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\one"
:: Build list of directorynames
SET "dnames="
FOR /f "delims=" %%a IN ('dir /b /ad "%sourcedir%" ') DO (
IF EXIST "%sourcedir%\%%a\docs\" SET "dnames=!dnames! "%%a""
)
FOR %%a IN (%dnames%) DO FOR %%b IN (%dnames%) DO IF NOT %%a==%%b (
FOR %%s IN ("%sourcedir%\%%~a\docs\*") DO (
IF NOT EXIST "%sourcedir%\%%~b\docs\%%~nxs" ECHO(COPY "%sourcedir%\%%~a\docs\%%~nxs" "%sourcedir%\%%~b\docs\%%~nxs"
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(COPY to COPY to actually copy the files.
I'd suggest that you create a testing subtree of a few small subdirectories before releasing in anger. Remember that this "report" may say to copy the same file from dir1 and dir5 into dir3 but when released because the file would actually be copied from dir1 to dir3, the copy from dir5 would not occur (when the copy from dir5 is checked, the copy from dir1 would already have occurred.)
You could suppress the copy report by appending >nul to the copy line.
Note that this routine is oriented towards one-file-at-a-time and showing what should be done. This following routine should do the same thing, is shorter but doesn't provide an elegant testing structure:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\one"
:: Build list of directorynames
SET "dnames="
FOR /f "delims=" %%a IN ('dir /b /ad "%sourcedir%" ') DO (
IF EXIST "%sourcedir%\%%a\docs\" SET "dnames=!dnames! "%%a""
)
FOR %%a IN (%dnames%) DO FOR %%b IN (%dnames%) DO IF NOT %%a==%%b (
ECHO(xcopy /d "%sourcedir%\%%~a\docs\*" "%sourcedir%\%%~b\docs\"
)
GOTO :EOF
Again, the xcopy is reported, not executed for testing purposes.
Using CMD line, in a given directory, I want to detect the most recently created/written folder and delete all the contents of that folder.
Any help/suggestions would be helpful.
This command prints all subdirectories in order of their last write/created time in reverse order (latest directories first):
DIR /A:D /O:-D /TW /B
To delete a directories' contents, a simple
DEL /S /Q "directory"
should be sufficient
If you want to process only the first result of the DIR command, you can use a FOR loop in a batch file, that leaves after the first iteration.
It should look something like this:
#ECHO OFF
REM delete all contents from the sub directory most recently created or written to
FOR /F "delims=" %%A IN ('DIR /A:D /O:-D /TW /B') DO (
RD /S /Q %%A
EXIT /B
)
Only works for the subdirectories of the current working directory, so use with care!
I guess for empty directories there will be some weird output, but I didn't test it.
EDIT:
Updated the batch file to remove the whole directory and its content using:
RD /S /Q "directory"