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.
Related
My ultimate goal is to individually zip subfolders of a main folder in one swoop from the top directory. So imagine this structure
Top-Folder
-Bob
--Folder-1
-Steve
--Folder-1
--Folder-2
-Tim
--Folder-1
From Top-Folder, I'd like to run a batch script that individually zips up each of the sub folders. So in the above example, I'd have four zip files, for each of those sub folders. They would reside alongside the original folders.
folder-1.zip (from Bob)
folder-1.zip (from Steve)
folder-2.zip (from Steve)
folder-1.zip (from Tim)
I have this so far:
if exist list.txt del list.txt
if exist subfolders.txt del subfolders.txt
REM create list of all users (top folders)
dir "F:\Top-Folder" /AD /B >list.txt
REM create list of all subfolders
for /F "delims=" %%i in (list.txt) do dir %%i /AD /S /B >> subfolders.txt
REM now you have all subfolders in subfolders.txt so you can zip them one by one
for /F "delims=" %%X in (subfolders.txt) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\"
With simple test folder names this is successful. However with my real folder names, whilst list.txt is generated with the top folder names, subfolders.txt is not created and ergo the zipping can't happen.
Examples of the real folder structure/names:
2005-05-11 - Tappehallerne, Copenhagen/[AUD #1] MD(M) - CDR(1)
2005-05-11 - Tappehallerne, Copenhagen/[PRO #1b] HDTV
What do I need to change to have the subfolder names written to the second text file for the rest of the script to execute?
for /F "delims=" %%i in (list.txt) do dir "%%i" /AD /S /B >> subfolders.txt
You need to "quote the name" otherwise dir looks for "quote" and "the" and "name"
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.
S:
cd \newclients
xcopy "s:\clients\*\MER" . /s /d
pause
This is my batch file. In my folder tree we have clients then names folders then MER folder. I want to be able to search the directory for the MER folder and copy that folder along with the client name before it. Is there a way to do this with batch files?
Using if exist should do the thing.
pushd "s:\newclients"
for /f "delims=" %%f in ('dir /b /ad "s:\clients\*"') do (
if exist "s:\clients\%%f\MER\nul" ( xcopy "s:\clients\%%f\MER" "s:\newclients" /s /d )
)
popd
pause
Note: Add a slash if it's to check folder ie. if exist "s:\clients\%%f\MER\" ... or if exist "s:\clients\%%f\MER\nul" ...
To suppress prompt about destination file/folder add /I in xcopy :
/I If in doubt always assume the destination is a folder
e.g. when the destination does not exist.
https://stackoverflow.com/a/33445866/
I am using the below but it is only moving the files and not the folders in the source folder.Is there anything I can add?
move "C:\source\*" "C:\destination\"
This is another way: test it on sample folders first.
robocopy "C:\source" "C:\destination" /move /s
EDIT: Robocopy copies the files and then deletes the original, so will take a long time for large files, even if the source and target locations are on the same hard drive.
#echo off
setlocal enableextensions disabledelayedexpansion
set "source=c:\source"
set "target=c:\destination"
(if not exist "%target%\" md "%target%" ) && (
pushd "%source%" && (
for /f "delims=" %%a in ('dir /a /b *') do move "%%a" "%target%\"
popd
)
)
Ensure that target folder exist, then, if source folder is accesible, change active directory to the source folder, and for each element inside it, execute a move operation to the target folder
I have a Windows Directory with about 8000 subdirectories. In each subdirectory there is a single zip file.
Is there a way I can batch script the move of each zip file to be moved to the parent directory, and then delete the subdirectory it was in? (Sub folder)
I know I can use xcopy to move files, but I'm not sure how to loop through the entire directory of sub directories.
Thank you!
#ECHO OFF &SETLOCAL
FOR /f "delims=" %%a IN ('dir /b /a-d /s *.zip') DO (
MOVE "%%~fa" ..
RD "%%~dpa"
)