0

I was figuring out a way to manage my downloaded files from chrome.I want that my pdf file should save in ABC directory, audio file should save in XYZ directory etc.I thought a way to do so through batch file.My batch file will run silently in background and will automatically distribute the downloaded files in required folders according to file type.

For html file type, as it save a directory too which contains a lot of stuff related to that html page.So I have to move both the .html file and associated directory as well.

I wrote a code to do so, but I am facing a problem here.It moves the .html file but not moving its respective directory.On command prompt it shows 1 dir(s) moved but actually directory is not moving.

Here is the code :

    @echo off

    setlocal EnableDelayedExpansion

    for %%i in (*.html) do (
    move "%%~i" HTML
    set dirName=%%i
    set dirNameWithoutExtension=!dirName:~0,-5!
    move %dirNameWithoutExtension% HTML

    )

Please suggest if any simple way is there to serve the purpose of distributing downloaded files according to file type.

  • Those set's are not needed, see %%~ni will get you the file name without extension which, coincidentally is also the paired directory name. These references can be found at for /?. Also, it's good practice to put a backslash after the directory name, since some command do mistake HTML for a file when they expect HTML\ for directory. Also, when using this type of copy method you might want to think about file and/or folder locks. What if the browser locks one of them down? You can't move them. Suggestion: use pre-made utilities. – JasonXA Apr 11 '15 at 9:31
0
@echo off
setlocal EnableDelayedExpansion
if not exist HTML md HTML
for %%i in (*.html) do (
    move "%%~i" HTML
    set dirName=%%i
    set dirNameWithoutExtension=!dirName:~0,-5!
    move "!dirNameWithoutExtension!" HTML
)

You can make a generic batch file to handle different extensions, or just use one of the many utilities created for just this purpose.

0

Try using relative paths, without detangling your For statement, can you try

%%~i\..\

and subsequent

%%~i\..\..\..\ 

walking yourself back up to the root if need be?

  • Hi Sam, I don't need to go up to the root because I am running the loop on one directory only.All the needed files are located in this directory. – user3189612 Apr 11 '15 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.