I want to move all the folders starting with "Temp_*****" to a different folder. It does not seem like we can use wild card with Folders. I was looking online, and someone posted this piece of code but I'm not sure how to apply it to my scenario.
#echo off for /d %%a in ({*}) do xcopy "%%a" "C:\Home\a\b\tmp\%%a\" /E
Here's one way to do it, replace C:\TEST01\ with your source folder location:
for /F %%a in ('dir C:\TEST01\TEMP_* /ad /b') do move C:\TEST01\%%a C:\Home\a\b\tmp\%%a
Related
so this has been puzzling me
I need to help to make this ('Dir /D /S A:D')
work with ren "New Folder" New Files"
I have tried different ways, but I just can't get the folders renamed
First attempt
#ECHO OFF
FOR /D %%d IN ('Dir /D /S A:D') DO (
REN "New Folder" "New Files"
)
Second attempt
#echo off
FOR /D %%D IN ('Dir /D /S A:D') DO RENAME "%%D\New Folder" New Files
Third attempt
#echo off
FOR /D %%D IN ('Dir /D /S A:D') DO CALL :RENAME %%D
:RENAME
SET CRITERIA=\New Folder
FOR /D %%R IN (%1%CRITERIA%) DO RENAME %%R "New Files"
what I want to do is rename folders. I have many Folders and Subfolders
and every main Directory folder has different names so the script needs to read past every folder and find New Folder and rename it to New Files
layout
Main Folder
---- Folder 1
------| New Folder
---- Folder 2
------| Folder 22
----------| New Folder
New folder is found in many different locations so the script needs to be able to read main folders, sub folders, and so on
any help with this will great
Updated Question
#echo off
for /f "tokens=*" %%f in ('dir /s /b a:d "New Folder"') do (
set "filename=%%~nf"
ren "%%f" "New Files"
)
Before
Main Folder...
Rename folder.bat
....| New Folder
......| New Folder
........| New Folder
Bad RESULTS
Main Folder...
Rename folder.bat
....| New Folder
......| New Files
........| New Folder
What it should do
Main Folder...
Rename folder.bat
....| New Folder
......| New Files
........| New Files
Need help with this
I even tried this
#echo off
for /f %%f in ('dir /s /b /a:d "New Folder"') do ren "%%f" "New Files"
Firstly, you seem to confuse for /D with for /F, and it is the latter you need to use here:
for /F "tokens=*" %%I in ('dir /S /B /A:D-H-S "New Folder"') do (
…
)
This iterates through the whole directory tree rooted at the current working directory (which is not necessarily the container of the batch script!). The tokens=* option of for /F ensures that white-spaces in the paths are maintained (removing leading ones, but such are anyway not possible for paths). The switch /A:D-H-S of dir ensures that files, as well as hidden and system directories, are excluded.
But there is one more problem: dir /S returns the items in a per-branch basis from top to the bottom of the hierarchy, and for /F, together with dir, lets the whole directory tree be built before loop iteration begins. So when now renaming a certain directory in the loop body, any children of it get new paths, although the old ones are buffered, letting the renaming fail. For instance, when directory D:\Path\To\Root\New Folder becomes renamed to D:\Path\To\Root\New Files, a sub-directory D:\Path\To\Root\New Folder\New Folder does no longer exist, since its path is now D:\Path\To\Root\New Files\New Folder, hence it cannot be found.
To overcome this issue, simply reverse the whole directory tree to be iterated by sort:
cd /D "D:\Path\To\Root" & rem // (use `%~dp0.` to specify a path relative to the container of this script)
for /F "tokens=*" %f in ('dir /S /B /A:D-H-S "New Folder" ^| sort /R') do (
ren "%%f" "New Files"
)
That way, the sub-directory D:\Path\To\Root\New Folder\New Folder is renamed first, before its parent D:\Path\To\Root\New Folder becomes processed.
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\"
)
I’m trying to copy a subfolder of one folder into a number of other folders with unknown names. The intention ist to backup the source file of a program in all employees folders who use it. If the programs folder isn’t found in an employees folder, nothing should be done. This looks as follows:
Source:
F:\Users\myFolder\programFolder\Sourcefolder
Target:
F:\Users\anotherOnesFolder\programFolder\Sourcefolder
So my idea was to do the following:
xcopy "F:\Users\myFolder\programFolder\Sourcefolder" "F:\Users\*\programFolder\Sourcefolder" /e /y
But this wildcard doesn’t seem to work. I’ve found a lot about wildcards at the end of the path, but that doesn’t apply here.
for /d %%d in ("F:\Users\*") do (
if /i not "%%~nxd"=="myFolder" if exist "%%~fd\folder\programFolder\Sourcefolder" (
robocopy "F:\Users\myFolder\programFolder\Sourcefolder" "%%~fd\folder\programFolder\Sourcefolder" * /mir
)
)
for /f %a in ('dir /ad /b "F:\Users*"') do (
xcopy F:\Users\myFolder\programFolder\Sourcefolder "%~dpfa\folder\programFolder\Sourcefolder" /e /y
)
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...
I need a script that will delete certain folders only. Say a dir contains the folders "Programming, Pictures, Movies, Crap" and lets say I want to keep ONLY the "Programming" and "Crap" folders kind of like adding them as exclusions.
What i DONT want to do is specify the folders to be deleted like:
set trashFolder=%userprofile%\Desktop\Test\M…
set trashFolder2=%userprofile%\Desktop\Test\…
rmdir /Q %trashFolder%
rmdir /Q %trashFolder2% <----i dont want this.
Lets say I know what folders to KEEP and I dont know whats in that folder so I make a script that specifies what folders to KEEP and delete the rest of the files/folders in that dir.
27 minutes ago - 4 days left to answer.
I pretty much want like a whitelist of folders that I want to keep NOT knowing what else is in that dir. and of course delete everything else except for the folders of that whitelist.
There are many ways this could be done. Here is one simple method.
for /d %%F in (*) do if /i "%%F" neq "Programming" if /i "%%F" neq "Crap" rd /q /s "%%F"
If you have a list of folders to keep in a whitelist file named "keep.txt", one folder per line, then you could do something like:
for /f "eol=: delims=" %%F in ('dir /b /ad^|findstr /vxilg:"keep.txt"') do rd /q /s "%%F"