Results 1 to 5 of 5

Thread: Stuck with some batch code

  1. #1
    Jay
    Jay is offline
    Gentlemen.. we're history Jay's Avatar
    Join Date
    Aug 2006
    Location
    Jita
    Posts
    8,365
    Thanks
    304
    Thanked
    568 times in 409 posts

    Stuck with some batch code

    Here is the code

    Code:
    @echo off
    setLocal DisableDelayedExpansion
    pushd "C:\Users\username\Desktop\New folder (2)\test"
    
    for /f "tokens=* delims= " %%G in ('dir/b/od *.html') do (set newest=%%G)
    copy %newest% "C:\Users\username\Desktop\New folder (2)\default.html"
    
    for /f "tokens=* delims= " %%G in ('dir/b/od *.rtf') do (set newest=%%G)
    copy %newest% "C:\Users\username\Desktop\New folder (2)\default.rtf"
    
    for /f "tokens=* delims= " %%G in ('dir/b/od *.txt') do (set newest=%%G)
    copy %newest% "C:\Users\username\Desktop\New folder (2)\default.txt"
    
    for /f "tokens=* delims= " %%G in ('dir/AD /OD /B') do (set newest=%%G)
    copy %newest% "C:\Users\username\Desktop\New folder (2)\default"
    
    POPD
    This finds the newest html, rtf txt and folder in "C:\Users\username\Desktop\New folder (2)\test" and copies it to "C:\Users\username\Desktop\New folder (2)\" also renaming each file / folder to "Default"

    It all works fine apart from the folder copy. I get "The system cannot find the file specified" Can anyone spot an issue here?

    Thanks
    □ΞVΞ□

  2. #2
    Jay
    Jay is offline
    Gentlemen.. we're history Jay's Avatar
    Join Date
    Aug 2006
    Location
    Jita
    Posts
    8,365
    Thanks
    304
    Thanked
    568 times in 409 posts

    Re: Stuck with some batch code

    Update on this I think its because of the spaces in the folder name

    I think that this would fix it

    for /f "tokens=* delims= " %%G in ('dir/AD /OD /B') do (set newest="%%G")
    copy %newest% "C:\Users\username\Desktop\New folder (2)\default"
    □ΞVΞ□

  3. #3
    Registered+
    Join Date
    Apr 2014
    Posts
    20
    Thanks
    0
    Thanked
    0 times in 0 posts

    Re: Stuck with some batch code

    I'll have a closer look, but before I do I thought I'd mention http://ss64.com/nt/ it's a great resource for cmd/batch. I use it all the time!

  4. #4
    Bagnaj97
    Guest

    Re: Stuck with some batch code

    Stick quotes around %newest% in the copy, otherwise if %newest% contains a space it'll cause the copy to choke.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Location
    BSOD CENTRAL
    Posts
    11
    Thanks
    0
    Thanked
    2 times in 2 posts
    • jcgriff2's system
      • CPU:
      • Intel Core i7 4700MQ
      • Memory:
      • 12 GB DDR3
      • Storage:
      • 1 TB
      • Graphics card(s):
      • Intel 4600
      • Operating System:
      • Windows 8.1

    Re: Stuck with some batch code

    Quote Originally Posted by Bagnaj97 View Post
    Stick quotes around %newest% in the copy, otherwise if %newest% contains a space it'll cause the copy to choke.
    Agree.

    Also, you may want to substitute the environment variable %userprofile% for C:\Users\username

    Regards. . .

    jcgriff2
    MS MVP

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •