I'm looking to move only .xls files out of a directory (including subfolders) and not .xlsx or .xlsm files, I found a command that I thought would do the job and it works fine if I specify .xlsx and will only pull through files with that extension.
But when specifying .xls it pulls through .xls, .xlsx, .xlsm and .xlsb even though I don't have a trailing wildcard.
for /r "c:\source" %x in (*.xls) do move "%x" "c:\destination"
I'm not that familiar with this so I'm probably just missing something obvious but have tried a search and cannot find anything to resolve this issue.
Any help would be appreciated. Thanks
The *.xls pattern is checked against both the long file names that are visible to the user and the short 8.3 file names that exist in the background by default and are intended for backwards compatibility. The short file names have extensions of 3 characters only, so a file with the long name longfilename.xlsx has a short file name extension .xls.
To overcome this behaviour, the easiest solution is to establish an additional filter by findstr:
for /F "delims= eol=|" %x in ('dir /S /A:-D /B "C:\source\*.xls" ^| findstr /I "\.xls$"') do move "%x" "c:\destination"
Related
So I am trying to create a batch file that will take a pdf file in the same directory as the batch file and output the file name (sans extension). I used this code to accomplish this:
#echo off
for /r "C:\Users\me\Test Folder" %%G in (*.pdf) do set "name=%%~nG"
This works fine. The next step is to search another directory and find a directory within the searched directory whose name matches the output of the above code (stored in the %name% variable). Here's what I tried:
dir "P:\Accounting\Acc Pay" | find %name% | set "loc=%%~dp"
The goal of the above code was to find only the directories that had the same name as the original pdf file and then set the drive and path of the output to a variable %loc%. I think this is where I messed up.
Once the path to the folder is set to %loc%, I then am supposed to finish with this line:
move .\*.pdf %loc%
This would take all the pdf files (there will only be one in the directory at once) in the directory with the batch file and move it to the path currently stored in the %loc% variable.
In total the code looks like this:
#echo off
for /r "C:\Users\me\Test Folder" %%G in (*.pdf) do set "name=%%~nG"
for /r %%A in ('dir "P:\Accounting\Acc Pay" | find %name%') do set "loc=%%~dpA"
move .\*.pdf %loc%
However, the code seems to move the pdf file into the same location it was already in (ie the folder with the batch file). I assume the %loc% variable is not working properly. Any help much appreciated.
Like #Magoo said (^|). And you surely want to add the following switches for the dir command: /b for "bare format" (name only) and /ad for "Attribute Directory" to return folder names only. find needs its's argument quoted, and for safety, the destination for the move command should also be quoted. Your find could benefit from /i to make it case-insensitive.
I personally would do it with nested loops to avoid creating superflouos variables:
#echo off
for /r "C:\Users\me\Test Folder" %%G in (*.pdf) do (
for /r %%A in ('dir /b /ad "P:\Accounting\Acc Pay" ^| find /i "%%~nG"') do (
move "%%G" "%%~dpA"
)
)
Bonus: should there be more than one .pdf file (maybe after the Weekend or Holidays), this would process all of them correctly in one go.
Depending on your naming structures, consider replacing find /i "%%~nG" with findstr /iblc:"%%~nG" (see findstr /?to find out what the switches mean)
(Note to prevent confusion: findstr is the only command (as far as I'm aware) that supports concatenating switches into one. /iblc is the same as /i /b /l /c)
I have two photo folders taken with different cameras, and want to merge them by date, and then view the combined directory in date sequence. I have merged them, and the directory dates look good, but the tools I use insist of showing the photos in name sequence, which is not what I want. I thought of doing a batch rename function to make the date part of the file names, using a bat file, but the DOS DIR command doesn't seem to use this date - if I do
for /f "skip=5 tokens=1,2,4,5* delims= " %%i in ('dir /a:-d /o:d /t:c') do (
etc., most of the file dates are correct, but some of the files seem to be using the "created date" of the directory.
Maybe there is a simple solution as this must be a common problem, but I haven't found a good solution - short of doing a laborious manual rename of some 700+ files!
Help would be appreciated!
Paul M.
for /f "tokens=1*delims=[]" %%a in ('dir /b /a-d /od /t:c^|find /v /n "" ') do echo ren "%%b" "%%a%%~xb"
Perform a directory scan of the source directory, in /b basic mode /a-d without directories in date order selecting created-date, number in brackets each line that doesn't match an empty string and assign each filename found to %%b and the number to %%a
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
Note: filenames that begin [ or ] will be processed incorrectly. If an existing file has a name which belongs to the target nameset (ie 1.? 2.? etc.) then simply prefix the target name with a prefix that doesn't already exist, eg ren "%%b" "new_%%a%%~xb" (where there are no existing files named new_*)
I'm not sure about Win7's capability when displaying files using the utility you are using (Explorer?) Win10 has the capability of selecting display by creation-date (select sort by then creation-date)
If you are sure you are sorting by creation-date then probably you'd need to select some other sorting scheme (change t:c to t perhaps)
If there are filenames that aren't being converted, then you are probably adding files (called img*) to a directory that already contains files named 1,2,3... - in fact, it's difficult to see how that phenomenon could happen otherwise.
Change "%%a%%~xb" in the rename command to "xyz%%a%%~xb" and you should find that the files are then all renamed xyz1,xyz2,xyz3 etc. If you then reprocess the files with xyz removed, the names should become 1,2,3 with no omissions.
I have a folder which contains a hierarchy of subfolders. The subfolders at the lowest level contains files.
Now, I'm interested in a .bat file (for Windows) which takes all files of a specific name, e.g. test.log, in the folder hiearchy and concatenates them and makes a new file in the position where the .bat is called.
How is this possible?
Thank you very much.
(for /f "delims=" %%a in ('dir /a-d /s /b "c:\root\folder\test.log"') do type "%%~fa") > "%~dp0\joined.log"
Use dir to list the required files, and for each file in the list, type it and send all the output to a file inside the batch file folder.
I have a list of file extensions I need to collect all the files of from a particular directory while maintaining attributes, timestamps etc. which has resulted in my usage of Robocopy. I'm hoping someone can suggest a more efficient method than my current solution?
At present I copy these files using the following command into an "unprocessed" directory:
robocopy %Directory1% "%Directory2%\unprocessed" /Z /E /copy:dat *.pst *.ost *.doc *.docx *.pdf *.docm *.xls *.xlsx *.ppt /log+:%Directory%.txt
The individual collected files from inside the "unprocessed" directory are then sorted to individual folders named after each file extension and created at the same level as the "unprocessed" directory, again using individual Robocopy commands:
robocopy "%directory2%\unprocessed" %directory2%\pst *.pst /Z /E
...
...
robocopy "%directory2%\unprocessed" %directory2%\ppt *ppt /Z /E
As you can appreciate, this results in unnecessary iterations of the "unprocessed" directory multiple times. I'm unable to copy files straight to the final resting places due to the nature of robocopy so I'm hoping someone can suggest a more suitable solution that will still allow file attributes to remain intact.
(I apologise in advance that this could turn into a discussion as opposed to someone being directly able to answer this)
Given the % signs in your examples, it looks like you're probably running this as part of a batch file. Either way, if you have the environment variables defined, you could use the following:
Command line:
for %e in (pst ost doc docx pdf dcom xls xlsx ppt) do robocopy "%Directory1%" "%Directory2%\%e" /Z /E /copy:dat *.%e /log+:%Directory%.txt
Bat file:
for %%e in (pst ost doc docx pdf dcom xls xlsx ppt) do robocopy "%Directory1%" "%Directory2%\%%e" /Z /E /copy:dat *.%%e /log+:%Directory%.txt
One other thing to note: You'll probably end up with all docx files in both doc and docx, likewise xlsx would end up in both xls and xlsx. Windows always picks up longer extensions with 3 character extensions.
I have a bunch of video files with names like so:
6592110904-Ivory-2.mp4
6592280588-Cornflower.mp4
6592321696-Ballet Pink.mp4
I want to rename them to get rid of everything after the first hyphen so they end up like:
6592110904.mp4
6592280588.mp4
6592321696.mp4
How do I go about doing this?
Please put the code below in a bat file, place it in directory with mp4 files. Before running real renaming, please remove "echo" before "move". please be carefull with renaming bacause (theoretically) it is possible to have same name for different files.You'll be prompted to confirm if you want to override the old one.
Code splits each filename after dash and renames the file taking first item. Good luck.
#echo off
for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.mp4"') do (
echo move "%%a-%%b" "%%a%%~xb"
)