This batch script is supposed to map to PC and rename the files in that folder using the date & time stamp and copy them to another location on a different PC. It should then delete all the files in that source folder except for a file that called "LBBS.log". It all works fine except for the delete part. It isn't deleting anything in the folder and it is actually deleting the batch file itself. When I run it, it copies over fine but then it deletes itself. Can someone please tell me what I need to change for this to work. What am I missing? Its on a windows 7 environment. Thanks in advance.
net use x: \\MTLLBBS023\C$
set "stamp=%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,2%"
set "source=MTLLBBS023"
xcopy /S /E /I x:\logs E:\Data\Logs\MTLLBBS023\%source%-%stamp%.*
cd x:\Logs
for %%i in (*) do if not %%i == LBBS.log del %%i
net use x: /delete
The problem is that your script and target directories are located on separate drives.
When you cd to another directory, the command will fail if you try to move to another drive without using the /d option.
Instead of cd x:\logs, you should say cd /d x:\logs - this will change the drive and directory.
Alternatively, instead of the net use and net use delete commands, you can simply pushd \\MTLLBBS023\C$ to go to the network drive (this also automatically creates a temporary network drive) and then popd at the end of the script to leave the directory and remove the mapped drive. This way, you don't need to cd at all.
Related
I would like to delete everything inside a folder on a network drive, but not the folder itself. It's school drive, therefore I can't delete the folder and replace it. If I try to delete the folder, it will look like i deleted it, until I restart my pc. I need to have access to the folder afterwards, and a restart in between is just too inconvenient.
Things I've tried:
cd \\LOCATION
rd . /q /s
However this error occurs
CMD does not support UNC paths as current directories.
I hope I explained this well enough, if there was something you didn't understand then just write, then I'll try to clarify.
Create a batch file and loop through every folder inside the folder you want to keep. Consider you have \\LOCATION\Folder\folder2 then folder3 etc. then this will remove all folders from within \\LOCATION\Folder but keep Folder:
set "myunc=\\LOCATION\Folder"
pushd "%myunc%" && (
for /d %%i in (*) do rmdir "%%i" /q /s
popd
)
Set sets a variable name of myfunc with a value which in this case is your path.
For /d is a loop that goes through each directory inside your %myunc% path and then simply do a rd on each.
Pushd allows you to pretty much cd to a network UNC path. pushd will create a temp drive letter for the UNC path. So it is almost like you doing net use X: \\servername\path
popd will then just remove the temp drive letter for you.
Open cmd.exe and type pushd /? and popd /?
To learn more on batch commands, from cmd.exe do help which will list all cmd commands, for each command you can run the /? switch to learn more about it.
Open the Start Menu and in the text box, type cmd.exe and hit Enter (or open the command prompt using your preferred method)
Switch to the network drive by typing Z: (where Z is the letter of the network drive)
Change to the parent directory of the directory you're trying to delete using cd path\to\parent\directory
Delete the directory using rmdir /S giantdir
For example, if you want to delete the directory O:\MG\WTF\BBQ\SOMANYFILES:
C:\Documents And Settings\Me> O:
O:> cd MG\WTF\BBQ
O:\MG\WTF\BBQ> rmdir /S SOMANYFILES
Or now that I think about it, I think you could just do
C:\Documents And Settings\Me> O:
O:> rmdir /S MG\WTF\BBQ\SOMANYFILES
but you would miss out on the chance to see Windows spell out OMGWTFBBQ in terminal font ;-)
By the way, rmdir (or del) does not move things to the Recycle Bin, it just deletes them, so be careful not to delete things you don't really want to.
I recently refreshed my PC and upon finally finishing, I realized a pesky file named thumbs.db had been left in every folder and directory. I'm trying to find a simple way to DELETE ALL thumbs.db from my ENTIRE C: drive.
Code I've tried (Only removed thumbs.db from desktop)
dir thumbs.db /a /b /s
del thumbs.db /a /s
PS: I'm open to any Power Shell Ideas Also.
You can achieve this by running:
C:
cd /
del /s /q /f /a:h Thumbs.db
The first line sets the drive you want to run it on.
The second line points to the root directory of the drive.
The third line force deletes (/f) hidden (/a:h) thumbs.db without prompting (/q) in all subfolders (/s)
I'm creating a batch file that deletes all Rar$DIa0.??? folders in the %TEMP% directory.
Is this possible, and how to do it?
The ??? is three randomized numbers. And that's where I have trouble with - deleting all folders that have Rar$DIa0. in the name.
for /d is designed for just this type of use. Something like this should work (remove one of the % if you're testing from the command line):
for /d %%i in ("%TEMP%\Rar$DIa0.???") do rd "%TEMP%\%%i"
The /d makes it work on directory names instead of file names.
If you want to make it easier on yourself, change to the %TEMP% folder first:
pushdir
cd /d %TEMP%
for /d %%i in ("Rar$DIa0.???") do rd "%%i"
The ??? makes it only act on folders that have three letters after a .. If your folders don't have just a three letter extension, change .??? to .*. If you've got a typo, and there is no actual . in the foldername, just remove it and use Rar$DIa0??? or Rar$DIa0*
You may want to test it first by changing rd to echo to make sure you get the folders you want before actually deleting them.
For more information about for (pun intended) type for /? from a command prompt.
The command line to use in a batch file for this task is:
#for /D %%I in ("%TEMP%\Rar$DIa0.*") do #rd /Q /S "%%I"
Command FOR with option /D searches in folder defined by environment variable TEMP for subfolders with folder name starting with Rar$DIa0. not having hidden or system attribute set.
The loop variable I holds for each found subfolder matching this folder pattern the name of found folder with full path without double quotes although the path to temp folder very often contains 1 or more spaces.
For that reason just the command RD with the parameters /Q for quiet execution and /S for deleting also all subfolders in the specified folder must be called with referencing the current value of loop variable I enclosed in double quotes.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
for /?
rd /?
By the way: WinRAR deletes the temporary folders usually automatically, except a file is opened from within an archive for viewing/modifying it in another application and WinRAR is closed before the other application is exited with the opened file. In this case WinRAR can't delete the temporary folder with temporarily extracted file because the file is still opened in another application. Of course also command RD can't delete the temporary folder if this folder is still the current directory of another application or a file in this folder is still opened by another application with a read/write access lock.
I want to use a batch file to synchronize 2 folders on Windows, and I'm using the xcopy command like this
xcopy /s /d <Original Folder> <Destination Folder>
It's working perfectly because I just want to overwrite only the modified ones, but I have 1 tiny problem, when I delete a file from the original folder, that file remains in the destination folder when i run the batch file, breaking the synching I want to achieve... I think this can't be done with xcopy, but I don't know which command I should use in case I delete a file in the original folder.
EDIT: This programs is for people who don't understant a lot of PC's so i don't want to install anything in their pc's. Just want a plain batch file to keep the 2 folders synchronized
SOLUTION: Thanks to Kuba Wyrostek I got to this command
robocopy <Original Folder> <Destination Folder> /e /purge
Thank you all for your time
xcopy has no such option. Your alternatives are:
rsync - http://rsync.samba.org
robocopy - http://technet.microsoft.com/en-us/library/cc733145.aspx
To refine your answer, you can simply do:
robocopy <Original Folder> <Destination Folder> /mir
/mir mirrors a directory tree (equivalent to /e plus /purge).
The XCOPY command for copying the updates from one folder to another is as follows:
C:\> XCOPY C:/SOURCE D:/DESTINATION /E /D /C /Y
The flags used in the above example have the following meanings:
/E causes all folders and sub-folders to be copied
/D causes a DATE comparison to be made, only copying items that are newer than the DESTINATION item. If the DESTINATION is older, or does not contain the file, then it will be copied.
/C tells XCOPY to continue if it encounters an error - typically errors occur with read-only files, or files that have protected permissions
/Y tells XCOPY to suppress prompting you for confirmation, which is necessary if you intend to create an automation script or scheduled task
I know this is old, but I have a question. Can this not be done with the correct xcopy commands.
Folder 1: c:\workingFolder\"contents here"
Folder 2: d:\workingFolderSync\"contents here"
Batch file below
xcopy "c:\workingFolder\*.*" "d:\workingFolderSync\" /i /s /d /y
xcopy "d:\workingFolderSync\*.*" "c:\workingFolder\" /i /s /d /y
pause
( I usually remove the pause after i test and know the batch works. )
But this will only copy contents from the folder to the other if its newer.
Then it looks in the destination folder and copies contents from destination back to source if it is newer. This reproduces what syncing does, except for the in real time monitoring of modified files. Creating a proper task schedule can actually implement running this command at specific times to almost perfectly reproduce syncing. You can also run this before modification on a machine and after modification on a machine. Even doing the same thing on a second machine if your using a network drive to sync.
Your method will have two main problems:
Files deleted from one folder will be reinstated by the copy left in the other folder the next time you run your xcopy
Renaming files can cause chaotic results.
I know the title doesn't make sense so I'll try to elaborate:
I have a program called My App.exe and when I run it, it creates a file in a folder in AppData. The name of the folder it creates varies for every machine. Example:
When My App.exe is run on one computer, it creates this file:
%LocalAppData%\Kazankoph\My_App.exe_Url_3p43oix65iqigwb4lndfi4m34sf1xjg3\1.2.0.0\Settings.dat
When its run on another machine it creates this file:
%LocalAppData%\Kazankoph\My_App.exe_Url_6f47ntd26lwubpr3hunvt2m67sf1xtq2\1.2.0.0\Settings.dat
The only things that stay the same are the parent folder "Kazankoph", the child folder "1.2.0.0" and the file name settings.dat
It seems that the text that comes after "My_App.exe_Url_" is always 32 characters long, and is randomly generated
So my Dilemma:
I need to create a command (using cmd, batch, or SciTE script) that will take the updated settings.dat file and overwrite the old one.
I used XCOPY with the Y switch to overwrite without prompt:
XCOPY settings.dat "%LocalAppData%\Kazankoph\My_App.exe_Url_6f47ntd26lwubpr3hunvt2m67sf1xtq2\1.2.0.0" /y
But the problem with that code is that it only works for that one computer.
I want a code that will work on any computer regardless of the 32 character code.
something like this:
XCOPY settings.dat "%LocalAppData%\Kazankoph\My_App.exe_Url_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\1.2.0.0" /y
Of course this one doesn't work but you get the idea.
Anyone have any suggestions?
Run this and it should give you the folder, and copy the file from the current directory into the folder.
#echo off
for /f "delims=" %%a in (' dir /b /s /a-d "%LocalAppData%\Kazankoph\Settings.dat" ') do set "folder=%%~dpa"
echo copying settings.dat to "%folder%"
copy /y "settings.dat" "%folder%\"
pause