Batch file to create subfolder in other subfolders, but only at top level - windows-7

I have a similar problem to this post, but I couldn't get the solution to work (I'm a total novice).
I have a set of folders on a network drive, each folder named after a manager (e.g. \sfnetfile\DEPT\HR_Manager\Manager, Test and \sfnetfile\DEPT\HR_Manager\Manager, Test 2). I am trying to get a folder/subfolder combo created in each manager folder of 2013\Variable. In some cases managers already have a 2013 subfolder, but not the Variable sub-subfolder (excuse the silly terminology).
I also want to prevent the batch file from creating the 2013\Variable folder in existing other subfolders within each manager folders (so I don't want it, for example, creating another level down: \sfnetfile\DEPT\HR_Manager\Manager, Test 2\2012\2013\Variable.
Here is my code:
FOR /d %A IN (\sfnetfile\DEPT\HR_Manager*) DO mkdir "%A"\2013\Variable

Something like this should do:
#echo off
for /d %%d in (\\sfnetfile\DEPT\HR_Manager\*) do md "%%~d\2013\Variable" 2>nul
assuming that sfnetfile is the name of a remote server and DEPT is a shared folder on that server. If sfnetfile is a top level folder on a mapped network drive, change that to:
#echo off
for /d %%d in (X:\sfnetfile\DEPT\HR_Manager\*) do md "%%~d\2013\Variable" 2>nul
where X: is the drive letter of the network drive.
Edit: As #dbenham correctly mentions, redirecting error output (to suppress error messages for existing leaf folders) will also suppress any other error message. A more sophisticated approach would check if the folder doesn't already exist and only then create it:
#echo off
setlocal EnableDelayedExpansion
for /d %%d in (\\sfnetfile\DEPT\HR_Manager\*) do (
set "folder=%%~d\2013\Variable"
if exist "!folder!" (
if not exist "!folder!\" echo "!folder!" exists as a file! 1>&2
) else (
mkdir "!folder!"
)
)

Related

How to auto create folder based on filename and move the file into it's folder using .BAT

I have already solved my question... What I haven't solved is how to do this if the .bat file is located in a parent folder and that it should work on all subfolders?
Right now, there's a limitation that it only create folders if the .bat file is located in the same folder as the files. It can't create folders if the files are inside a subfolder.
What I have is:
the filename of this .bat is :
organize.bat
#echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
md "%%~ni" && move "%%~i" "%%~ni"
)
)
How I do it right now:
I place the .bat file in a folder together with the files
When I click it, it will create folders with a name based on the files inside that folder
It will also move each files in those folders of the same name
What I need:
Place the .bat file in the main folder with many subfolders containing the files
Click it to perform the same tasks above
Apologies if my explanation is confusing... I hope it's still understandable.
Thank you in advance!
Your attempt is very close to working but beware the wrinkles of using a simple approach without checking each detail so, start here:-
#echo off & Title %~n0
REM I recommend using cd /d "%~dp0" to ensure you start from the known cmd file folder location not some system folder
cd /D "%~dp0"
REM add the /R switch to run through subdirs
for /R %%i in (*) do (
REM replace organize to %~n0 so as to aid renaming by other users
if not "%%~DPni" == "%~DPn0" (
REM to allow for nested paths we need a fuller DP location for N (check it works exactly as desired then remove the echos)
echo md "%%~DPni" && echo move "%%~i" "%%~DPni"
)
)
BEWARE files with double .dot.s such as cmd.exe.lnk so check those echo's first
md "C:\Users\me\Favorites\Links\cmd.exe"
move "C:\Users\me\Favorites\Links\cmd.exe.lnk" "C:\Users\me\Favorites\Links\cmd.exe"

How to search/delete multiple subfolders?

I have a "clean up" batch program that sits in a parent directory & deletes certain extraneous files within the directory "tree".
One function is to delete extra ".backup" save folders (just .backup, not main folders)
For reference, the folder path is like:
\Parent folder (w/ my .bat)
\Saves
\save 1\save files
\save 1.backup\save files
\save 2\save files
\save 2.backup\save files
(etc)
I can remove the unwanted subfolders using
for /d %%x in (Saves\*.backup) do rd /s /q "%%x"
However, I want to add a check into the code to verify that the folders have actually been deleted & give correct message for success, failure, no folder, etc. outcomes.
I can do that with file deletion functions using IF EXIST/IF NOT EXIST arguments, but I'm having trouble applying these to the subfolder deletion.
I can look for files in the save folders to satisfy the EXIST/NOT EXIST arguments, but using the * wildcard in the subfolder name seems to be throwing a wrench into the works, and the program fails to detect any folders/files using that statement. However if I define a specific save folder outright, the subroutine will work perfectly.
So:
if not exist "Saves\save 1.backup\*.*" ( goto nofile )
if exist "Saves\save 1.backup\*.*" ( for /d %%x in (Saves\*.backup) do rd /s /q "%%x" )
will delete the correct folders (or give a message if no valid folders), but
if not exist "Saves\*.backup\*.*" ( goto nofile )
if exist "Saves\*.backup\*.*" ( for /d %%x in (Saves\*.backup) do rd /s /q "%%x" )
doesn't delete anything, & just gives my "nofile" message.
Obviously, specifying every possible save name isn't an option.
Any solutions?
p.s. to head off any extra questions: I want to [occasionally] delete these backups because the program makes a new one each time it saves.
You can check folder into for loop after command rd.
for /d %%x in (Saves\*.backup) do (
rd /q /s %%x
if exist %%x (echo Failed) else echo Success
)

Batch file: moving specific files from subfolders to a destination folder (Windows)

I'm new to writing code / commands, and I was hoping for some assistance.
I need to search and copy specific files within a folder and its subfolders.
I have the following script (that I use when all of the files are in one source folder):
#echo off
set src_folder=C:\Users\macca\Downloads
set dst_folder=C:\Users\macca\Downloads\Testing
set file_list=C:\Users\macca\Downloads\photo_test.txt
if not exist "%dst_folder%" mkdir "%dst_folder%"
for /f "delims=" %%f in (%file_list%) do (
xcopy "%src_folder%\%%f" "%dst_folder%\"
)
pause
I've tried to google this, and search on stack overflow, but I'm not finding anything (I suspect that I am using the wrong terminology) .

copy paste files using CMD

I want to accomplish the following: copy File A from Directory A to Directory B but File A already exist in Directory B and i don't want to overwrite it, simply put the new file in Directory B with a (1) or something so i'll know the difference. i can't manually name the file in the script as i plan to make it a batch file and run it every few hours therefore resulting in the same file existing with different names. IE. File A, File A(1), File A(2) so on and so on. please help. Thank you
Use Xcopy command with parameter /D
C:\> xcopy D:\source_dest E:\target_dest /E /D
/E parameter make that it copy files with subfolders
/D parameter make that it copy files without overwrite
if want more help inform me .if it works vote up
A cyclic copy refers to the situation where the source directory
contains the destination directory. Since the destination directory
is part of the source, the copying process eventually starts copying
the destination directory to a deeper portion of the destination.
This process will continue until, of course, the finite disk space
eventually runs out.
To avoid this condition but to achieve the objective, one can
specify a temporary destination which is on another volume (e.g.,
D:\temp\) and later copy the temporary destination to the final
destination, and delete the temporary directory at the end.
just check file exist before run xcopy command then run copy command
if NOT EXIST c:\directory B\File A ( xcopy c:\Directory A\File A c:\Directory B )
The following is made for folders but you can modify it for files.
Makes a backup of existed folder directory (and all contents in it) with new increase number in folder name [like TEST(1)... TEST(2)... folder].
The first part :Start will set the %PathFolder% variable with path to folder name (%userprofile%\Desktop\TEST) and then will search if exist. If NOT exists, will create it (xcopy), else, If exist, will directing the %PathFolder% variable to :Search label (for-)loop section for further handling...
Tip: Can be used %1 variable to set PathFolder set "PathFolder=%1" so it will work when you Drag-n-Drop a folder on this current saved batch file.
The second part :Search will search in the %PathFolder% variable (%userprofile%\Desktop\TEST) and will make a COPY of "TEST" folder (and all contents in it) with an increase number added in parenthesis at the end of folder name [like TEST(1)]. If already exist TEST(1) then will copy TEST folder as TEST(2) ... or TEST(3) ... and so on.
::Make a backup of existed folder directory (and all contents in it) with new increase number in folder name.
#echo off
setlocal EnableDelayedExpansion
set "IncrNum=1"
:Start
::The following will set the "%PathFolder%" variable with path to folder name (%userprofile%\Desktop\TEST) and then will search if exist. If NOT exists, will create it (xcopy), else, If exist, will directing the "%PathFolder%" variable to :Search label (for-)loop section for further handling...
::Tip: Can be used %1 instead of %userprofile%\Desktop\TEST in [set "PathFolder=..."] line so it will work when you Drag-n-Drop a folder on this current saved batch file.
set "PathFolder=%userprofile%\Desktop\TEST"
if NOT exist "%PathFolder%" (
xcopy %PathFolder% %PathFolder% /i /y
exit /b
) else (
goto :Search
)
exit /b
:Search
::The following will search in the "%PathFolder%" variable (%userprofile%\Desktop\TEST) and will make a COPY of "TEST" folder (and all contents in it) with an increase number added in parenthesis at the end of folder name [like TEST(1)]. If alredy exist TEST(1) then will copy TEST folder as TEST(2) ... or TEST(3) ... and so on.
for /f "tokens=*" %%G in ('dir /b /s /a:d "%PathFolder%*"') do (
if exist %%G^(%IncrNum%^) (
echo At "%%~dpG" a folder "%%~nG(%IncrNum%)" alredy existed.
set /a IncrNum+=1
goto :Search
) else (
echo.
echo.
echo The "%%~nG" folder and all contents in it
echo will be copied now as "%%G(%IncrNum%)".
echo.
pause
xcopy %%G %%G^(%IncrNum%^) /i /y
exit /b
)
)
:EndBatch
set "PathFolder="
pause
exit

How can I create new empty folders in a directory using names of subfolders of another directory using batch script?

My main goal is to backup SVN repositories in REPOS folder. Since "svnadmin hotcopy" has to have both source and target folders I need to create new folders in different directory with name of folderName_backup and then copy them using "svnadmin hotcopy". Btw this has to be done in windows batch file. My code for this portion is the following:
for /d %%X in (%source%\*) do (
md %destination%\%%X_backup
svnadmin hotcopy %%X %destination%\%%X_backup
)
After running this code I get error : The filename, directory name, or volume label syntax is incorrect.
set "source=c:\where\the\folders\are"
set "destination=c:\where\the\backup\will\be"
for /d %%a in ("%source%\*") do for %%b in ("%destination%\%%~nxa_backup") do (
if not exist "%%~fb\" md "%%~fb"
if exist "%%~fb\" svnadmin hotcopy "%%~fa" "%%~fb"
)
%%~fb is the full path to the file/folder referenced in %%b. The equivalent for %%~fa. %%~nxa is the name and extension of the file/folder referenced in %%a.
The code encloses all paths in quotes to avoid problems with spaces, and test for existence of the folder before creation and backup
You can remove the inner for %%b and use the composed target folder in the md and svnadmin commands (as in your code), but this aditional for allows to write only once how the target folder is defined.
for /f "tokens=*" %%G in ('dir /b /a:d "%Source%"') do (
md %destination%\%%G
svnadmin hotcopy %Source%\%%G %Destination%\%%G
)
The problem with the old solution was instead of getting folder names in directory it gets subdirectory of the directory. This is why the result was something like C:\where\the\backup\will\be\C:\where\the\folders\are
That was the reason that I got error:The filename, directory name, or volume label syntax is incorrect. The script part (works fine) that I have shared with you above in answer tokenize the subdirectory to get the folder name and use it.

Resources