A batch file to move the most recent folders first? - windows

A batch file to move the most recent folders first?
For example i have a batch file which moves folders from my local machine to a remote machine, is there a way to move the most recent created folders first?
Currently i have the following:
xcopy /s C:\Users\xxxx\Desktop\AutoFrameworkResults \\192.1xx.1.xx\xxxx\xxxxx\Common\AutoFrameworkResults
thanks for your help

I might be too late to the party but here is the script anyway.
setlocal
set "sourceDir=c:\users\xxxx\desktop\autoframeworkresults"
set "destDir=\\192.1xx.1xx\xxxx\xxxx\common\autoframeworkresults"
for /f "delims=" %%d in ('dir "%sourceDir%" /o-d /ad /b') do (
xcopy /s "%sourceDir%\%%d" "%destDir%\%%d\"
)

Related

Using FOR in a batch file for deleting specific files at once

I'm trying to search for specific files in all directories and subdirectories inside my batch file path in order to delete them at once :
for /F "delims=" %%A IN ('dir /B /S %~dp0bb1-*.png bb2-*.png bb1_0*.png bb2_0*.png style.css *.thumb 2^>nul') DO del /q "%%A"
What do you think of this code ? Are there things that could be changed/improved ? Is it reliable and safe from unwanted deletion ?
Any help is appreciated.

Batch to move files outside of randomly-named subfolders

I have a data set consisting of files organised according to the following hierarchical folder/subfolder structure:
I would like to remove all nuisance subfolders (move its contents outside of it at the same hierarchical level + delete the nuisance folder), thus ending up with the files organised like this:
How can I achieve this, using a batch file, run from a command prompt inside Windows 7? I've tried a number of for statements with %F and %%F, but none worked. Grateful for any tips.
I believe the following will accomplish your goal if and only if all child folder and file names are unique. If there are duplicates, then all hell will break loose.
I have not tested, so backup and/or try the code on disposable data first.
You will have to modify the first PUSHD command to point to the root where all your "person n" folders reside.
#echo off
pushd "yourRootWherePersonFoldersReside"
for /d %%U in (*) do (
pushd "%%U"
for /f "eol=: delims=" %%A in ('dir /b /ad 2^>nul') do (
for /d %%B in ("%%A\*") do (
for /d %%C in ("%%B\*") do (
md "%%~nxC"
for /d %%D in ("%%C\*") do move "%%D\*" "%%~nxC" >nul 2>nul
)
)
rd /s /q "%%A"
)
popd
)
popd
The second FOR loop must be FOR /F instead of FOR /D because FOR /D has the potential to iterate folders that have been added after the loop has begun. FOR /F will cache the entire result of the DIR command before iteration begins, so the newly created folders are guaranteed not to interfere.

Deleting all files except the most 3 recent in a folder and sub folders

To clean up our back-ups folder I have written a short batch file that will be run Via task scheduler to periodically clear all files except the 3 most recent copies.
Currently I have the following
for /f "skip=3 eol=: delims=" %%F in ('dir /b /o-d *.sqb ^| findstr /r /c:"LOG_.*"') do #del "%%F"
This will work for the files inside the folder with the batch file, however I would like to run it from a higher level to check all the backups, I have tried the /s command but these only leaves the 3 most recent out of ALL folders where as I need to keep the 3 most recent files in EVERY folder and subfolder
Loop recursively by directories first:
for /r /d %%a in (*) do (
for /f "skip=3 eol=* delims=" %%b in ('dir /b /a-d /o-d "%%a\*LOG*.sqb"') do (
del "%%~fb"
)
) 2>nul

Windows batch file - how to loop through files in a directory?

I have this batch:
for /f %%a IN ('dir /b *.pdf') do call convert.exe %%a
This gets every pdf file thats in the same folder as convert.exe. I want to be able to say where the PDF resides. What do I have to change?
Thanks!
If the directory name can be hardcoded, then it will be
for /f %%a IN ('dir /b /s "Disk:\Your\Directory\Name\*.pdf"') do call convert.exe %%a
Note that this will also return all .pdf files in subdirectories of Disk:\Your\Directory\Name.

cmd: Find updated folder and delete it using cmd

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"

Resources