3

This should be simple not sure why it isn't working... I have a batch file with 2 cmds and am trying to get it to write the output to a log file.

Here is what is in my batch file:

PUSHD "\\My Very Long\Exact Directory\" >>logfile.txt
for /f "skip=7 eol=: delims=" %%F in ('dir /b /o-d *.xlsm') do del "%%F" >>logfile.txt

The PUSHD is because this file is in on a remote file storage \\My Very Long\Exact Directory\ and I want the actions to be taken within the directory where the batch file is, and not the directory that it is being called from. The second command cleans up the directory by deleting old copies.

When I run the batch, by either calling it from the remote server, or by double clicking it straight away, the cmd window opens and runs the clean up. It will create a file in the directory called 'logfile.txt' but it is empty.

The process that calls this batch file from the remote server is scheduled, and I want to see what happened when this was called later. How do I get the results to write to the log file. I imagine it should look very similar (it not the same) as what the cmd window pops opens and shows.

I reference this other post: Redirecting Output from within Batch file


Adding more as I have been informed PUSHD nor DEL have outputs... I put a pause in the file to capture the cmd window. I am looking to capture this information and of course any errors that occur. Maybe this is the wrong approach?

X:\File Path\BackUps>PUSHD "\\My Very Long\Exact Directory\"  1>>logfile.txt
Y:\File Path\BackUps>for /F "skip=7 eol=: delims=" %F in ('dir /b /o-d *.xlsm') do del "%F"  1>>logfile.txt
Y:\File Path\BackUps>del "2015 CSLD Weekly Comment Analysis 2015-10-10  9_11_09 .xlsm"  1>>logfile.txt
Y:\File Path\BackUps>del "2015 CSLD Weekly Comment Analysis 2015-10-10 23_55_29 .xlsm"  1>>logfile.txt
Y:\File Path\BackUps>pause
Press any key to continue . . .
Community
  • 1
  • 1
AdrianBoeh
  • 176
  • 2
  • 3
  • 14
  • 3
    Your log file is empty because neither `pushd` nor `del` outputs anything to the screen. Only things that are displayed on the screen get sent to a file when you use `>` and `>>`. – SomethingDark Oct 12 '15 at 23:51
  • @Mofi Perfect thank you! I particularly appreciate your attention to a detailed yet concise answer. I believe I have provided you credit for the correct answer. Again, really great help, thank you! – AdrianBoeh Oct 13 '15 at 22:40

1 Answers1

2

You could use following batch code to redirect stdout and stderr to a log file.

@echo off
>>logfile.txt 2>&1 (
    echo INFO: Making "\\My Very Long\Exact Directory\" the working directory ...
    PUSHD "\\My Very Long\Exact Directory\"
    echo INFO: Deleting old *.xlsm files in this directory ...
    for /f "skip=7 eol=: delims=" %%F in ('dir /b /o-d *.xlsm') do (
        echo INFO: Deleting file "%%F" ...
        del "%%F"
    )
)

See also the Microsoft TechNet article Using command redirection operators.

>>logfile.txt redirects stdout into a log file by appending every text output to stdout (standard output) to the file. The log file is created new only if not already existing.

2>&1 redirects stderr (standard error) to stdout to get finally standard messages - the lines with echo command - as well as error messages output by the used commands into the log file.

Mofi
  • 46,139
  • 17
  • 80
  • 143