1

I have a question I hope you will be willing to answer. I have a fair amount of experience writing BASH scripts, but know next to nothing about Windows scripting. So my question may be very basic. I have a script that asks for user input and uses that input to create two directories. Then the script moves .mp3 files into the newly created directories. So far so good.

The problem is that I then want to copy the newly created directories (along with the files they contain) to my MP3 player. ( D:\MUSIC),

What happens when I run the script, however, is that the mp3 files are copied to the destination directory individually - that is they end up not in a copy of the directory they were copied from, but just in the D:\MUSIC directory.

In other words I end up with: D:\MUSIC\file.mp3 instead of D:\MUSIC\new_directory\file.mp3

Below is my script: I would be grateful if you could give me a clue as to what I am doing wrong.

Thank you,

GG

    @echo off
set /p FOLDER= Name the folder
cd C:\Users\John\Music\iTunes\iTunes Media\Podcasts\"Coast to Coast AM Podcast" 
MD %FOLDER%
Move *.mp3 %FOLDER% 
Copy %FOLDER% D:\MUSIC
CD C:\Users\John\Music\iTunes\iTunes Media\Podcasts\"Darkness Radio"
MD %FOLDER%-DR
Move *.mp3 %UserInputName%-DR
Copy "%FOLDER%-DR" D:\MUSIC
GrouchyGaijin
  • 111
  • 1
  • 3
  • 1
    Might be helpful if you add the bash code for reference. For example I wonder why the script asks for folder name interactively as it doesn't seem too handy. Also it's not clear why you create the `%folder%` on `c:\.....`. And look at `cd /d` option, could be useful. – wOxxOm Oct 24 '15 at 18:08
  • 1
    Take a look at window's `xcopy` and/or `robocopy` - they are more advanced. Also, you might want to install Cygwin so you can use your bash scripts on windows. – Kenney Oct 24 '15 at 18:12
  • @wOxxOm Basically the reason for interactively asking for the name of the directory is that I was unsure how to create a directory using a date in the past as the name of the directory. When I originally started this it just didn't occur to me to create the directory on D as the files I want to move are downloaded to C. – GrouchyGaijin Oct 25 '15 at 07:26
  • @wOxxOm I guess my question boils down to: In a script, when you are referring to a directory name using a variable, what is the syntax to copy that directory and its contents to another location? – GrouchyGaijin Oct 25 '15 at 10:45
  • Then put that new info into the question. Can you edit it? – wOxxOm Oct 25 '15 at 13:10

1 Answers1

0
  1. To create a directory with the today's date as its name use the locale-independent method:

    wmic path Win32_LocalTime get Day,Month,Year /format:table
    

    Day Month Year
    25   10      2015

    To put it into a variable use for /f parse-loop that skips the header line, puts the next 3 tokens into %%a, %%b, %%c, pads MONTH and DAY with 0 and extracts the last two digits (01—>01, 011—>11).

    for /f "tokens=1-3 skip=1" %%a in ('
        wmic path Win32_LocalTime get Day^,Month^,Year /format:table
    ') do if not "%%c"=="" set "YEAR=%%c" & set "MONTH=0%%b" & set "DAY=0%%a"
    set TODAY=%YEAR%.%MONTH:~-2%.%DAY:~-2%
    

    2015.10.25

    Note: , and = in the command line inside for /f needed escaping with ^, the same goes for &, |, >, < (we didn't use them here).

  2. Now let's declare a list of folders to process in a plain for loop, each item will be:
    source folder name without path : target folder postfix, which can be split at : in two new loop vars:

    for %%a in (
        "Coast to Coast AM Podcast:"
        "Darkness Radio:-DR"
        "No colon here so the postfix will be empty too"
    ) do for /f "delims=: tokens=1,2" %%b in ("%%~a") do echo SRC: %%b, POSTFIX: %%c
    

    SRC: Coast to Coast AM Podcast, POSTFIX:
    SRC: Darkness Radio, POSTFIX: -DR
    SRC: No colon here so the postfix will be empty too, POSTFIX:

    Note: the text to parse must be doublequoted by default for /f %%a in ("some text") do but in our case %%a always contains a doublequoted string so we were able to use in (%%a).

  3. xcopy /s "SOURCE" "TARGET\" will create the target directory (note \ at the end) if needed.

  4. The final code:

    @echo off
    set "COPY_FROM=%USERPROFILE%\Music\iTunes\iTunes Media\Podcasts"
    set "COPY_TO=D:\MUSIC"
    
    rem Use a locale-independent method of getting the date
    for /f "tokens=1-3 skip=1" %%a in ('
        wmic path Win32_LocalTime get Day^,Month^,Year /format:table
    ') do if not "%%c"=="" set "YEAR=%%c" & set "MONTH=0%%b" & set "DAY=0%%a"
    set TODAY=%YEAR%.%MONTH:~-2%.%DAY:~-2%
    
    for %%a in (
        "Coast to Coast AM Podcast:"
        "Darkness Radio:-DR"
    ) do for /f "delims=: tokens=1,2" %%b in (%%a) do (
        xcopy /s /d /y "%COPY_FROM%\%%b\*.mp3" "%COPY_TO%\%TODAY%%%c\"
    )
    pause
    

    Note: xcopy /d copies only the newer files, /s processes subdirectories, /y silently overwrites the existing files.

Community
  • 1
  • 1
wOxxOm
  • 65,848
  • 11
  • 132
  • 136