I using the following syntax to move all the files inside a folder, to another folder
set OGGI=%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%
move C:\DOCUMENTALE\dms_in\*.* C:\DOCUMENTALE\dms_backup\%OGGI%\
This syntax is working with files, but which syntax I have to use if I would move also folders?
The problem is that this path C:\DOCUMENTALE\dms_in\ contains folders automatically generated. I don't know how many folders there are in or which names they have.
I did some attempt but I didn't figure it out
Thanks
An solution to this is to use the command ROBOCOPY instead. It is arguable more flexible.
ROBOCOPY <source> <destination> /MOV /E
/MOV will move the file/folder in question instead of the default copy.
/E will allow you to mirror the directory structure of the target folder if there are any folders within the folder including empty ones
Note: you may need to use quotation marks for the source and destination if either path contains spaces.
Another neat feature of Robocopy is that it will show you what is it doing; what is being copied and too where, how long it takes etc. And if it goes wrong due to paths for example, it will tell you.
You can also browse ss64.com for more help and examples to fit your needs.
Related
We are in the process of migrating files from one share to another. We have built a tool in which the user can select directories and/or individual files to be copied to a destination share. The tool generates an individual RoboCopy command for each of the files or directories in the collection that results from the selection made by the user.
We are having problems if an individual file to be copied starts with a dash, for instance:
robocopy c:\temp c:\temp2 -a.txt
RoboCopy bails out with: ERROR : Invalid Parameter #3 : "-a.txt"
We tried the usual suspects (quotes around the filename etc.), but so far nothing seems to work. Any idea how to get around this, without resorting to renaming the file prior to copying?
This appears to be a bug in robocopy; it has some other known similar ones:
https://support.microsoft.com/en-us/kb/2646454
Here's a possible workaround:
robocopy c:\temp c:\temp2 *-a.txt /xf *?-a.txt
*-a.txt will still match "-a.txt", but it also matches "x-a.txt", "xx-a.txt", etc.
The /xf file exclusion knocks out "x-a.txt", "xx-a.txt", and any other file with characters (specifically, at least one character) in front of the hyphen.
I've confirmed that the above command will match only "-a.txt" even if c:\temp also contains these files:
other folder\-a.txt
-a.txt1
-a1.txt
x-a.txt
xx-a.txt
I'm not 100% confident though, so you might want to think up some other filenames to test that against.
I feel like this should be straightforward, but I've been struggling with this for long enough that it's making me pull my hair out. I'm writing a couple batch files to do automated file syncing and some cleanup between a few different drives. When copying a directory I use the following command.
robocopy %datestr% %2 /E
What this line of code does is take the contents of the path specified by %datestr% and copies it to the path specified by %2. I need it to copy the directory itself (and its contents, of course) to the path specified by %2. I looked at the Robocopy docs and I can't seem to find a simple way to do this.
EDIT: I fixed the problem I was having, but it feels more like a hack than a fix. Appending the source folder to the destination path 'copies' it, as such:
robocopy %datestr% "%2/%datestr%" /E
I don't love the fact that this isn't truly 'copying' it, but just making a new directory with the same name. For what I'm using this it will work fine, but I still feel as if there must be a better way to do so.
Problem
I'm trying to move a folder (ex. \\myUNC\folder1) and its contents to another folder (ex. \\myUNC\Parent\folder1). There are two easy enough ways I'd typically do this - either using move (similar to here) or using ren (similar to here).
Example Code
set oldPath=\\myUNC\folder1
set newPath=\\myUNC\Parent\folder1
move "%oldPath%" "%newPath%"
::ren"%oldPath%" "%newPath%"
Troubleshooting
When attempting the move solution in my first like, I get the error:
The filename or extension is too long. 0 dir(s) moved.
As a result, I tried ren like in my second link, which gave the error:
The syntax of the command is incorrect.
For this second error, I'm assuming that is because I'm passing the path as part of my variable - which ren doesn't accept. The batch calling this change is NOT in the same directory as the folder or its new path. As a result I can't use current directory code (like ren or cd), at least as far as I know.
If anyone has a possible solution it would be greatly appreciated! Thanks.
The filename or extension is too long. 0 dir(s) moved.
This error refers to a 'feature' in Windows that limits file names to a maximum of 255 characters. To overcome this, you would need to shorten the names of the folders on the Network drive. See Maximum filename length in NTFS (Windows XP and Windows Vista)?
The syntax of the command is incorrect.
This error occured because you cannot state a new folder for the ren command. A file can only be renamed in the same folder, not even subdirectories are allowed. But the reason why you got The syntax of the command is incorrect. error, is because you left out a space in between the ren and the "
Possible solution:
Depending on your scenario, you might be able to pushd into the folder and then use the move command. In my experience, some commands don't respond equally to UNC locations vs local file locations (ex. if exist "\\UNC\"):
#echo off
pushd \\myUNC\
set oldPath=folder1
set newPath=Parent\folder1
move "%oldPath%" "%newPath%"
popd
This will only work though, if you haven't exceeded the 255 char limit
After a lot of troubleshooting, I was able to find a solution with no errors!
Solution
set oldPath=\\myUNC\folder1
set newPath=\\myUNC\Parent\folder1
robocopy /move "%oldPath%" "%newPath%"
Why
Robocopy is a newer command from Microsoft, and accounts for filename strings longer than 256 characters (apparently the issue with move and/or copy command).
You can google robocopy to learn more about the command options and parameters, but its fairly straight forward. For my issue, I wanted to move the file, so I just used the /move option, which deletes the original folder and files.
I am completely new to this, but I am trying to create a .bat file that will allow me to rename a pair of files within a designated folder and move them into a subfolder. The part I am having trouble with is that I am wanting a prompt to come up to identify/select the files to be renamed and moved.
Example file names are:
A1234, A1235, A1236, B1234, B1235, B1236, etc.
Is there a way to bring up a prompt that allows the user to type the shared name (ex 1234)of the files and rename and move both files to the designated subfolder?
Any and all help would be appreciated!
Suggested approach
for part of problem
part I am having trouble with is that I am wanting a prompt to come
up to identify/select the files to be renamed and moved. Is there a
way to bring up a prompt that allows the user to type the shared name
(ex 1234)of the files and rename and move both files to the designated
subfolder?
Do a search operation using wildcard, like "?1234" for the case highlighted above ( should be made generalized for all acceptable and expected patterns "*1234*" is the generic most )
Now do a RENAME inside a For loop on the results obtained by search.
As you suggest you are a newbie with Batch, following tutorials will help you build your file. Look for elements like Variables, For Loop
Batch Tutorial
Here you go
#echo off
set /p file=Please type shared name:
for %%a in (C:\Folder\?%file%.*) do (
move "%%a" subdir
ren "subdir\%%a" newname.*
)
I have a batch file I've created which uses xcopy to copy a dir, and child dirs, and merge them into one file. (ie. all my modulised development css stylesheets merged into one production stylesheet).
Everything is good, apart from the fact that when I reference the excludelist.txt, it only excludes the first line, and not the subsequent files I want excluded.
Anyone fancy a crack at this? You'd be most helpful.
Here's the code:
XCOPY C:\xampp\htdocs\PROJECT\css\*.* C:\temp /S /I /Y /EXCLUDE:C:\xampp\htdocs\PROJECT\exclude.txt
...and inside my exclude.txt is...
1.css
2.css
3.css
4.css
5.css
///// I know the code works (to an extent) because it is infact excluding file 1.css -- just not the ones below it. Am I right to put each exclusion on a new line?
I use the following,
xcopy "C:\users\dad\*.*" dad /s /d <yesnoyesno /EXCLUDE:excluexclu 1>cop.txt 2>err.txt
as somewhere on the web I saw a note to the effect that the /Y switch could not be used directly with an exclude file.
What I wanted to point out here, was the useful output files 1 & 2, which detail the success & failure issues.
Mine shows only success.
The short answer: Create a new text file, type the entries and save the file.
The longer explanation: I ran into this very issue on a Windows 2008 server today. I tried all kinds of things to get it to work. Forced notepad to save the file as ANSI, Unicode and UTF-8 but all of them had the same problem. Finally I started thinking about the fact that I put the file on the server via FTP and it's quite likely that FTP tweaked the file format. I created a new text file on the server, typed all the same entries in the new file and now it works.
I had a similar problem. I was trying to exclude all files with a certain extension in the root folder and any sub-folders and it didn't work. The reason was I was putting *.pdb instead of just .pdb. The newline/carriage return thing was a total red herring for me.
So my file just looked like:
.pdb
.obj
.vb
.cs
You seem to be using xcopy correctly - each exclusion file (or match depending on wildcards) should be on a new line within the file. So far I've been unable to reproduce the behaviour you're experiencing.
Is the exclude.txt listing you've given above a test file, or are they the actual css names?
What are the names of the other files that your batch file is supposed to copy?
Edit:
That the xcopy is failing to exclude further files after a single match is giving me most pause. I thought it might be to do with the type of carriage-return that was used in the exclude file, but xcopy handles unix-style carriage-returns just fine.
Can you re-verify that the correct exclude file is being used?
Try forcing it to save with ANSI encoding on your text editor.
I was having a similar issue and that did it.