just when i thought i knew batch i ran into trouble

Discussion in 'Software' started by red death68, Oct 20, 2011.

  1. red death68

    red death68 Command Sergeant Major

    i am trying to make a batch file that will create a rtf file in a several directories. the file should have the same name, and be named by a variable and here is where im finding trouble, it is not letting me name the file like
    Code:
    echo hi > %DOW%.rtf
    this has worked in windows xp but apparently 7 changed several things

    on a side note i want the files to be made blank but i can just edit their contents with a script if i need to

    here is what i have so far and im lost

    Code:
    For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    	Set Month=%%A
    	Set Day=%%B
    	Set Year=%%C
    )
    set /p "DOW = %Month%-%Day%-%Year%"
    
    echo hi = %DOW%.rtf
    pause
    ps: the files will be written in the directories

    c:\users\reddeath\desktop\classes\chemistry\
    c:\users\reddeath\desktop\classes\comm class\
    c:\users\reddeath\desktop\classes\lab\
    c:\users\reddeath\desktop\classes\math\
    c:\users\reddeath\desktop\classes\merlins class\
    c:\users\reddeath\desktop\classes\world history 1\

    pss: i will also be making a batch that will run at like 7pm or something(scheduled task) and check if the contents of the file = anything if so the file will stay if the file is empty it will be deleted
     
  2. GermanOne

    GermanOne Guest

    Look at the differences.
    yours:
    Code:
    set /p "DOW = %Month%-%Day%-%Year%"
    
    mine:
    Code:
    set "DOW=%Month%-%Day%-%Year%"
    
    I guess you know what the set /p is good for...
    The major fault in your code is that you mustn't place any spaces around the equal sign. Otherwise the result is a variable
    %DOW % (note the space)
    with a value
    " 10-20-2011" (without the quotes).

    The same for echo hi > %DOW%.rtf. The space between hi and > is also part of the line. Get rid of the spaces here. Also (to make sure it would work for a single figure at the end of the line) I recommended umpteen times to write echo-redirections in an opposite style
    Code:
    >%DOW%.rtf echo hi
    
    It looks a bit odd but it always works fine.

    Regards
    GermanOne
     
  3. red death68

    red death68 Command Sergeant Major

    i see thats weird iv always used weird spacing in xp and never had a problem i guess iv just been lucky till now ill try the modified code and let you know how it goes
     
  4. red death68

    red death68 Command Sergeant Major

    my code works now except for some reason i cant make the file in one of my directories
     
  5. GermanOne

    GermanOne Guest

    I place a bet you forgot to enclose a path in quotes or you did it the wrong way ;)
     
  6. red death68

    red death68 Command Sergeant Major

    sadly i doubt that the folder in question is called math here is the code

    Code:
    @echo off
    For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    	Set Month=%%A
    	Set Day=%%B
    	Set Year=%%C
    )
    set "DOW=%Month%-%Day%-%Year%"
    cd c:\users\reddeath\desktop\classes\chemistry\
    >%DOW%.rtf echo. 
    cd "c:\users\reddeath\desktop\classes\world history 1\"
    >%DOW%.rtf echo.
    cd "c:\users\reddeath\desktop\classes\merlins class\"
    >%DOW%.rtf echo.
    cd "C:\Users\reddeath\Desktop\classes\math"
    >%DOW%.rtf echo.
    note iv tried the path with both quotes and no quotes
     
  7. GermanOne

    GermanOne Guest

    Hmm, there is no backslash but that shouldn't make any difference.

    Try that shorter code:
    Code:
    @echo off
    for /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do set "DOW=%%A-%%B-%%C"
    pushd "%userprofile%\desktop\classes"
    for %%i in ("chemistry\" "world history 1\" "merlins class\" "math\") do >"%%~i%DOW%.rtf" type nul
    popd
    
    Regards
    GermanOne
     
  8. red death68

    red death68 Command Sergeant Major

    i will when im back at my laptop btw can you explain tokens and delims i never got that but iv seen it used alot(especially in code i splice)
     
  9. GermanOne

    GermanOne Guest

    Hope this code will explain it ...
    Code:
    @echo off
    echo delims means delimiter(s) or separator(s)
    echo   -the standard delimiters are space and tab
    echo   -you have to use "delims=" to avoid that a string will be splitted
    echo    on this characters
    echo   -you could use several delimiters
    echo   -all delimiters are removed from the string
    echo   -if a delimiter is followed by another delimiter they are
    echo    parsed like a single delimiter
    echo   -delimiters in the beginnig of a string are removed and ignored
    echo(
    echo tokens are the substrings
    echo   -if you need more than only the first token you have to define them
    echo   -the first (named) token will be saved in the FOR variable
    echo    you have defined
    echo   -the next (named) token will be saved in the variable with
    echo    the proximate letter (case sensitive!)
    echo   -an asterisk means that the entire rest of the string will be
    echo    saved in the next FOR variable
    pause>nul
    echo(
    set "string=#1#$2# #4$5"
    set string
    echo(
    echo for /f %%%%a in ("%string%") do echo "%%%%a"
    for /f %%a in ("%string%") do echo "%%a"
    pause>nul
    echo(
    echo for /f "tokens=1,2" %%%%a in ("%string%") do (echo "%%%%a" ^&echo "%%%%b")
    for /f "tokens=1,2" %%a in ("%string%") do (echo "%%a" &echo "%%b")
    pause>nul
    echo(
    echo for /f "tokens=2" %%%%a in ("%string%") do echo "%%%%a"
    for /f "tokens=2" %%a in ("%string%") do (echo "%%a")
    pause>nul
    echo(
    echo for /f "delims=#" %%%%a in ("%string%") do echo "%%%%a"
    for /f "delims=#" %%a in ("%string%") do echo "%%a"
    pause>nul
    echo(
    echo for /f "tokens=2 delims=#" %%%%a in ("%string%") do echo "%%%%a"
    for /f "tokens=2 delims=#" %%a in ("%string%") do echo "%%a"
    pause>nul
    echo(
    echo for /f "tokens=1* delims=#" %%%%a in ("%string%") do (echo "%%%%a" ^&echo "%%%%b")
    for /f "tokens=1* delims=#" %%a in ("%string%") do (echo "%%a" &echo "%%b")
    pause>nul
    echo(
    echo for /f "tokens=1-4 delims=#" %%%%a in ("%string%") do (echo "%%%%a" ^&echo "%%%%b" ^&echo "%%%%c" ^&echo "%%%%d")
    for /f "tokens=1-4 delims=#" %%a in ("%string%") do (echo "%%a" &echo "%%b" &echo "%%c" &echo "%%d")
    pause>nul
    echo(
    echo for /f "tokens=1-5 delims=#$" %%%%a in ("%string%") do (echo "%%%%a" ^&echo "%%%%b" ^&echo "%%%%c" ^&echo "%%%%d" ^&echo "%%%%e")
    for /f "tokens=1-5 delims=#$" %%a in ("%string%") do (echo "%%a" &echo "%%b" &echo "%%c" &echo "%%d" &echo "%%e")
    pause>nul
    echo(
    echo try more by yourself
    pause>nul
    
    Regards
    GermanOne
     
  10. GermanOne

    GermanOne Guest

    Forget about "standard delimiters". I mixed German and English.
    Standard-Trennzeichen = default delimiters
     
  11. red death68

    red death68 Command Sergeant Major

    ok that was very informative either way ty german
     
  12. red death68

    red death68 Command Sergeant Major

    sorry for late reply been insanly busy it works german tyvm now i just need to write the somewhat simple code for deleting the ones that dont get used at end of day after run(i hope 7 didnt mess with delete parameter....)
     
  13. GermanOne

    GermanOne Guest

    What would you like to check? Whether a file is still empty or when it was changed last?

    (BTW: I had a 5-day English seminar last week, but they didn't tell us anything about what 7 in "i hope 7 didnt mess ..." is for :-D Sometimes you've lost me because of such a silly thing.)

    Regards
    GermanOne
     
  14. red death68

    red death68 Command Sergeant Major

    mess means mess up or screw up lol and i was refering to windows seven cmd's

    and i was gonna check if the files were empty

    and then set the first batch to run at like 6am(while im sleeping) and then run the second batch at like 11pm when im watching tv or going to bed
     
  15. GermanOne

    GermanOne Guest

    Only the 7 was confusing. I guessed it's some kind of phonetics... :-D

    Simply run a FOR loop across the files and check whether %%~zi is zero.


    The key word is "scheduled task".
    But if all files are overwritten at 6 in the morning then it will not make any sense to delete the empty files before ...

    Regards
    GermanOne
     
  16. GermanOne

    GermanOne Guest

    EDIT
    Sorry, I forgot that the date is part of the file name...
     
  17. red death68

    red death68 Command Sergeant Major

    lol now you understand my plans lol

    and i will impliment your loop when i have free time

    and i knew the key words i was just to lazy to type them
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds