开发者

Issues with Batch based file/folder creation

开发者 https://www.devze.com 2023-02-23 02:38 出处:网络
First, I\'d like to thank anyone who can help in advance, I haven\'t done any batch scripts like this in years and being \'rusty\' is an understatement. I have a lot of files on a mapped drive that I

First, I'd like to thank anyone who can help in advance, I haven't done any batch scripts like this in years and being 'rusty' is an understatement. I have a lot of files on a mapped drive that I want to do some work with without risking damage to those files so I'm trying to create a local version of 'fake' files so no harm if something gets screwed up.

This is all being done on a Windows 7 machine, so I started with running the following to get the entire directory listing to manipulate:

dir /b M:\ > movies.txt

From there, if I run the following, I'm able to get individual 0 byte fake files based on the lines inside 'movies.txt':

for /F "eol=; tokens=1* delims=," %%i in (movies.txt) do @echo.> %%i.avi

Or if I run the following, I'll get empty directories based on 'movies.txt':

for /F "delims=~" %%f in (movies.txt) DO MD "%%f"

The problem with either of these commands is that I either get a pile of files all lumped into the root folder (first 'for' command) or I get empty directories (second 'for' command) but I can't seem to figure out how to get the fake files into the folders.

Examples: If Mapped Drive "M" has these movie files:

M:\Las Vegas Trip (2009)\Las Vegas Trip (2009).avi
M:\Las Vegas Trip (2010)\Las Vegas Trip (2010).avi

then 'movies.txt' will appear as:

Las Vegas Trip (2009)
Las Vegas Trip (2010)

and the first command will result in:

D:\Las Vegas Trip (2009).avi
D:\Las Vegas Trip (2010).avi

or the second command will result in:

D:\Las Vegas Trip (2009)\
D:\Las Vegas Trip (2010)\

But what I need is:

D:\Las Vegas Trip (2009)\Las Vegas Trip (2009).avi
D:\Las Vegas Trip (2010)\Las Vegas Trip (2010).avi

I've been searching the internet for days and can't seem to find anything that will give me the desired results... I did find a slightly similar 'issue' elsewhere where they wanted to create the folders based on file name, but they wanted to auto-rename the end result filename which wont work. (Yes I tried to modify their command to work, but no changes I could come up with would produce anything but errors.) You can see the above mentioned method here (on site).

Modifications I attempted to the code from the link included:

for /f %%f in ('dir *.png /b') do md %%~nf & move %f .\%%~nf\0000.png <- original
for /f %%f in ('dir *.avi /b') do md %%~nf & move %f .\%%~nf\*.avi
for /f %%f in ('dir *.avi /b') do md %%~nf & move %f .\%%~nf\
for /f %%f in ('dir *.avi /b') do md %%~nf & move %f .\%%~nf

In each case I was given an error in reference to the final part of the code "%%~nf*.avi" section.

Any ideas of how to either modify that code to work for me or something else that would work would be greatly appreciated.

-------Edited Part------- I tried changing my batch file to use the command provided in the post below and it didn't seem to work:

cd Movies2
dir /b %MovieSource% > movies.txt
for /f "delims=" %%f in ('dir *.avi /b') do md "%%~nf" & move "%~f" ".\%%~nf\*.avi"
rem for /F "eol=; tokens=1* delims=," %%i in (movies.txt) do @echo.> %%i.avi
rem FOR /F "delims=~" %%f in (movies.txt) DO MD "%%f"

I also tried putting the first 'for' command second and unREM'd the other 'for' command so that it would create the *.avi's beforehand, thinking that might work, but trying it either way g开发者_如何学Goives the same error.

The following usage of the path operator in batch-parameter
substitution is invalid: %~f" ".\%%~nf\*.avi"


Try this:

@ECHO OFF

SET oldroot=M:\
SET newroot=D:\newroot\
FOR /R "%oldroot%" %%f IN (*) DO CALL :processfile "%%f"
GOTO :EOF

:processfile
SET "oldfile=%~1"
CALL SET "newfile=%%oldfile:%oldroot%=%newroot%%%"
CALL :checkpath "%newfile%"
ECHO.>"%newfile%"
GOTO :EOF

:checkpath
SETLOCAL EnableExtensions
IF NOT EXIST "%~dp1" MKDIR "%~dp1"
ENDLOCAL
GOTO :EOF
  • This script iterates through all the files by the path specified in oldroot.

  • The old root path in each fully qualified file name is replaced with the path to the new root (the newroot variable), resulting in another fully qualified file name (stored in the newfile variable).

  • The new file's path is checked: if it does not exist, it is created.

  • Finally, an empty file with the new name is generated.

EDIT

The ECHO.>"%newfile%" line actually produces a file with an EOL character (0x0D0A) in it, so the size of such a file is 2, not 0. If you wish it to be 0, you can replace the ECHO bit with this construct:

SET /P somevaryrandomandunusedvarname=>"%newfile%" <NUL


It's seems to be the problem in the FOR /F statement, as it uses as standard delim <space> and <TAB>.

It should work with something like

for /f "delims=" %%f in ('dir *.avi /b') do md "%%~nf" & move "%~f" ".\%%~nf\*.avi"
0

精彩评论

暂无评论...
验证码 换一张
取 消