First post, and I have searched and tried for this but cant seem to find something like this.
Okay, so I have spent some time on this and have hit a wall. I understand and can code this if I am moving on a single drive (with move) but as soon as I try to implement xcopy so that it will work cross drives I begin to struggle.
This is what I have so far. This will look in the directory name for a left parenthesis and if it has it, it will try to move the directory to the other folder. This works correctly.
#echo off
SETLOCAL EnableDelayedExpansion
for /d %%f in ("%C:\Testing\Folder1%\*") do (
SET "folder=%%f"
if /I NOT "!folder:(=!"=="!folder!" move "%%f" C:\Testing\Folder2
)
Example directory names are as follows
Vacation (2004) - Move
Florida Vacation - Dont Move
New York (2014) - Move
Exmaple Folder - Dont Move
Now when I found out move does not work for different drives (D: (source) and E: (destination)) I found out I needed to use Xcopy. When I try to use xcopy instead of move it will never move any files or folders.
I am thinking I might have to mkdir the folder in the new location first and then move everything and delete the old one, but then I would need to substring the directory name from the full path. I probably can do it, but want to verify with you guys before I spend another half day googling how to do it. I have slammed my head into the wall enough for the past few days that I figured some experts could help out.
Attempt with xcopy
#echo off
SETLOCAL EnableDelayedExpansion
for /d %%f in ("%D:\Folder1%\*") do (
SET "folder=%%f"
if /I NOT "!folder:(=!"=="!folder!" xcopy "%%f" E:\Folder2
)
I appreciate any and all help you can give.
Jay
I use the following, perhaps it'll work for you:
#echo off
setlocal enabledelayedexpansion
for /d %%i in (C:\TEST\*(*) do (
xcopy "%%i" "D:\%%~pni" /s /i
)
pause
Substitute paths for your relevant directories. Good luck!
Related
Before I start posting code I am wondering if this is even feasible.
I have a directory of folders I need to move to a new directory.
But I only need to move the folders that contain only 2 files in them.
The rest of the folders have more than 2 files in them, but they need to stay.
So would this be feasible with a batch file?
This was interesting so I took a stab at it:
#echo off
set "dir=C:\Your\Current\Directory"
set "ndir=C:\Your\New\Directory"
setlocal enabledelayedexpansion
for /d %%A in (%dir%\*) do (
pushd "%%A"
for /f %%B in ('dir /a-d-s-h /b ^| find /v /c ""') do (
set cnt=%%B
if "!cnt!" == "2" (if not exist "%ndir%\%%~nA" robocopy "%%A" "%ndir%\%%~nA" /e)
)
)
pause
I kept running into issues so I modified a few things to make it do what I wanted; there're likely more elegant ways to go about it, but this worked ¯\_(ツ)_/¯. First thing is setting variables for your current directory (dir) and your new directory (ndir) to make it a little easier to digest later on; we also need to enable delayed expansion since the value of our counting variable (cnt) will change between loop iterations. The first FOR loop is /d, which will loop through folders - we set each of those folders as parameter %%A and use that to change our directory (using pushd) prior to running our nested commands.
The second FOR loop is /f, which will loop through command results - the commands in this case being dir and find. For dir we are specifying /a to show all files that -d aren't folders, -s system files, or -h hidden files, and we display that output in /b bare format. Using the output from dir, we run find and specify to /v display all non-empty lines and then /c count the number - which becomes parameter %%B.
Finally, we set %%B as our counting variable (cnt) - if !cnt! is equal to 2, we see if the folder already exists in the new directory, and if it does not we robocopy it over. The move command was giving me some trouble because the folder would be locked by the loop, so if you want you could also throw in a DEL command to delete the original folder.
Let me know if that helps! Hopefully your research was going well anyway.
References: Counting Files, FOR Looping, pushd, DIR, FIND, robocopy
Forgive me if this is a simple question, but I've been out of practice for a few years!
I am creating a .bat file at work that will require me to move all files from one directory to another and if any conflicts arise, to add the date to the filename instead of overwriting it. All in all, I want to keep every file, even if they are duplicates. TIA for any help!
MD "C:\NewFolder"
CD "%userprofile%\desktop"
For /f "delims=" %%A in ('dir /a-d /b "%userprofile%\desktop"') do If not exist C:\NewFolder\%%A (copy "%%A" "C:\NewFolder\%%A") Else copy "%%A" "C:\NewFolder\%%A%date:/=%"
It ugly but it works and should get you started. See dir /?, for /?, and if /?. Note you have to change directories for it to work.
I'm trying to tidy up a data folder and have written a batch file to take care of a lot of preliminary work (delete empty folders, delete junk files, etc), but I'm falling over when trying to deal with files within duplicate folders.
This is an example of the current situation:
w:\Data\Corporations\555\20130101\Concat_000001\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000002\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000003\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000004\555_20130101_data.zip
There should only be one Concat folder per YYYYMMDD folder, and should look like this:
w:\Data\Corporations\555\20130101\Concat\555_20130101_data.zip
There are hundreds of folders in w:\Data\Corporations to be processed, so I figure I need to first of all find any folder named Concat_*, make a folder named Concat within the same parent folder, and then move any zip from Concat_ to Concat.
I have tried various combinations of FOR /D in (Concat_*) with MD and MOVE commands, but with no luck so far. I've also tried calling a subroutine from the FOR statement that would jump back a level in the tree, create a folder named Concat, go back to Concat_* and move the .zip files, but again with no luck.
Any help would be greatly appreciated.
Cheers,
Pete
try this:
for /r "w:\Data\Corporations\555" %%a in (*.zip) do for %%b in ("%%~dpa.") do md "%%~dpbConcat" 2>nul & move /y "%%~fa" "%%~dpbConcat"
The following does what you asked. If you uncomment the last line, then empty config_* folders will be removed. Non-empty config_* folders will be preserved.
#echo off
for /f "delims=" %%F in ('dir /b /ad /s concat_*') do (
if not exist "%%~dpFconcat\" mkdir "%%~dpFconcat\"
if exist "%%F\*.zip" move /y "%%F\*.zip" "%%~dpFconcat\" >nul
REM uncomment line below if you want to remove empty concat_* folders
REM dir /b "%%F"|findstr "^" >nul || rd "%%F"
)
If that title wasn't confusing enough.. hopefully what I am trying to do is a lot simpler to understand.
Windows 7 just in case it needs to be said.
I have multiple directories within the folder I am working in;
C:\WorkingDir\1
C:\WorkingDir\2
C:\WorkingDir\3
and so on
Within each of these folders (1,2,3,etc) is a single sub-directory and no other files or folders;
C:\WorkingDir\1\5E04AB
C:\WorkingDir\2\4F07FC
C:\WorkingDir\3\9DA04F
I need to move each of those single subdirectories from within the parent folder to a new folder;
C:\NewFolder\5E04AB
C:\NewFolder\4F07FC
C:\NewFolder\9DA04F
That's it! I thought it might be simple, but I can't wrap my head around the variables or a better resource explaining how to use them. I don't use batch files much if at all, so I am very sorry for this cry for help. Hopefully someone more knowledgeable has a simple explanation that can help me out :-)
I found this: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
But can someone link me to a resource where I can learn more about batch variables and parameters for future reference?
Thank you thank you thank you
Update:
#endoro
Thanks for your response. There must have been a user error the first time I tried to run your code. It is working properly and all is well! Thank you so much!
Update 2
After running the code User1 contributed, It will create my NewFolder directory, but will not copy anything to it. It remains empty. Here is some of the repetitive output in DOS when I run it:
C:\WorkingDir>(
set fldr2=C:\WorkingDir\1\5E04AB
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.
C:\WorkingDir>(
set fldr2=C:\WorkingDir\2\4F07FC
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.
C:\WorkingDir>(
set fldr2=C:\WorkingDir\3\9DA04F
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.`
Please look at the output and remove the echo, if it is OK:
#echo off &setlocal
set "workingdir=WorkingDir"
md "C:\NewFolder" 2>nul
pushd "%workingdir%"
cd
for /d %%i in (*) do for /d %%j in ("%%~i\*") do echo move "%%~fj" "C:\NewFolder\%%~nxj"
popd
I haven't been able to test this but:
#echo off
setlocal EnableDelayedExpansion
md "c:\NewFolder\"
cd "C:\WorkingDir\"
for /D /r %%a in ("*") do (
set fld1=%%a
cd "%fld1%"
for /D /r %%b in ("*") do (
set fld2=%%b
move "c:\WorkingDir\%fld1%\%fld2%" "C:\NewFolder\%fld2%"
)
)
And a good resource for batch:
http://ss64.com/nt/
At work we handle people every day who wants help regarding signing on to their Online Bank from home. Some times we need the user to delete files and guiding them to do this, can be tiresome and bothersome a lot of the time, wasting upwards to 20 minutes because the user is inexperienced in computers.
We have been talking about a solution (if possible) where we let the user download a batch file that we tell them to run, which can then delete the required files for us. I ask this question here, because none of us here at work, have experience with batch files.
Personally I've played around with it from time to time but I can't really come up with a solution that fits our needs and when I search around on the net, I can't find a solution that fits either. The script would have to locate the folder automatically (if possible).
Is this possible?
Would you be able to help me accomplish this?
Thanks in advance!
Maybe this could help you, I commented it so you understand what it is doing ;)
:: Program to remove Folders which fullfill the criterias
:: You won't see the commands in the console
#echo off
:: Go to drive C (i think you'll need application data or similar and this is
:: on c, but if the bat is started in an other partition, it will search the
:: one he starts in
C:
:: Go to the path in which you'll search, if you want C:\ , remove this
CD "C:\yoursearchstartpath"
:: Okay, this is the loop, it gets all folders ( /A -D is for folders)
:: which have delMe in their name and then asks if you want to delete it
:: showing the path to make sure you don't delete sth accidentially
for /f "delims=" %%a IN ('dir /S /B /A -D *delMe*') do RD /S "%%a"
:: That the batch file is not closed automatically but shows that it's finished
set /p id="Finished, press Space to quit " %=%
A simple one:
rd /s "%USERPROFILE%\.oces2"
If they follow a pattern (e.g. start with .oces):
#echo off
cd /d "%USERPROFILE%"
for /d %%i in (.oces*) do rd /s "%%i"
pause
Check out this link
http://support.microsoft.com/kb/65994
I created a sample batch file A.BAT. It checks if the C:\Folder\F1 exists, and removed the folder F1 if available
IF EXIST D:\FOLDER\F1 GOTO A
EXIT
:A
D:
DEL D:\FOLDER\F1\*.*
RMDIR F1
Hope it helps