1

I have written the following .bat file, and it runs perfectly on my Windows 2000 machine, but will not run on my Windows 7 or Windows XP machines. Basically it just loops through the current directory and runs a checksum program which returns the checksum. The output of the program is saved to a text file and then formatted to remove the checksum of the output file.

@Echo Off

for /r %%f in (*.txt) do crc32sum.exe %%f >> all_checksums.txt

ren all_checksums.txt old.txt
findstr /v /e /c:"all_checksums.txt" old.txt > all_checksums.txt
del old.txt

When I run this file on my Win2k PC with a bunch of text files and the crc32sum.exe in a folder, it outputs the file. On other machines it outputs a blank file. I turned Echo on and kept only the for loop line and found that the output from executing the crc32sum.exe is nothing. If you manually run the crc32sum.exe file it outputs the checksum no problem.

Any ideas as to how to fix this?

EDIT: Here is a link to the software: http://www.di-mgt.com.au/src/digsum-1.0.1.zip

EDIT2: New development, it seems that the file works if the path of the folder has no spaces in it i.e. C:\temp or C:\inetpub\ftproot or C:\users\admin\Desktop\temp. Does anyone know how I can make this work with paths that have spaces? %%~f doesnt work it says unexpected.

  • My apologies, I think it is not a 16-bit DOS application as it runs just fine if I run it directly in the command line like this: crc32sum.exe file.txt It seems to only be causing trouble when run in this loop. In fact if I take echo off, I see no errors, but I can see 1>all_checksums.txt which I believe means the program is running, but just exiting without an output? i.e. return 1. By the way, I have the crc32sum.exe file in the same directory as the bat file and the text files, same in all computers. – Mishca de Costa Dec 09 '15 at 00:31
  • Also, none of the text files have spaces in their names, but I will try %%~f when I get to work tomorrow. – Mishca de Costa Dec 09 '15 at 00:33
  • Try running it without redirecting the output. – Squashman Dec 09 '15 at 01:07
  • @Squashman if I turn echo off, I see it is running crc32sum.exe file.txt 1>>all_checksums.txt so the exe is just returning 1. If I remove the redirect to the text file all I see is crc32sum.exe file.txt and then a new line. – Mishca de Costa Dec 09 '15 at 13:48
  • @Mofi using %%~f yields the error "%%~f" was unexpected at this time. In addition, I tried specifying the full path to crc32sum.exe but that didnt help either. – Mishca de Costa Dec 09 '15 at 13:52
  • One strange new development. I made a new folder on my desktop in Windows 7 and copied 3 text files, the unedited batch file and the exe to it. Now running the batch file worked. I coped the batch file and the exe FROM THAT FOLDER to the XP machine (also a new folder on desktop) and it wouldnt work (I used different text files). Then I copied the exact same text files, batch file and exe to the XP machine but it outputs nothing. I am at a loss as to why... – Mishca de Costa Dec 09 '15 at 13:58
  • 1
    New update, if I move the aforementioned folder to C:\temp it works fine. My thinking is that the spaces in the pathname is what is screwing it up, since for windows XP the desktop is under Documents and Settings, whereas with Win7 none of the folders in the path have spaces. Knowing this I should be able to avoid usage of the file in such paths, but does anyone know of a way to fix it? – Mishca de Costa Dec 09 '15 at 14:18
  • use quotes around all your variables that use directory paths and filenames. – Squashman Dec 09 '15 at 14:25
  • "THE path of THE folder" There is no mention of paths or directories in your program. Please post a sample of the output when the `crc32sum.exe` executes correctly. Do you mean "the current directory contains spaces?" – Magoo Dec 09 '15 at 14:26
  • @Squashman, thank you this worked perfectly, just had to add "" around the last %%f. – Mishca de Costa Dec 09 '15 at 14:34
  • @Magoo my apologies I should have done that. The issue is resolved now though, but I will keep this in mind for future questions. What I meant by spaces was in win xp the path is C:\Documents and Settings\... as you can see there are spaces in that path, and that is what was causing the error. – Mishca de Costa Dec 09 '15 at 14:34

1 Answers1

0

Try this modified batch code which worked on Windows XP SP3 x86:

@echo off
goto CheckOutput

rem Command DEL does not terminate with an exit code greater 0
rem if the deletion of a file failed. Therefore the output to
rem stderr must be evaluated to find out if deletion was
rem successful or (for a single file) the file existence is
rem checked once again. For details read on Stack Overflow
rem the answer http://stackoverflow.com/a/33403497/3074564

rem The deletion of the file was successful if file created
rem from output message has size 0 and therefore the temp
rem file can be deleted and calculation of the CRC32 sums
rem can be started.

:DeleteOutput
del /F "all_checksums.txt" >nul 2>"%TEMP%\DelErrorMessage.tmp"
for %%E in ("%TEMP%\DelErrorMessage.tmp") do set "FileSize=%%~zE"

if "%FileSize%" == "0" (
    set "FileSize="
    del "%TEMP%\DelErrorMessage.tmp"
    goto CalcCRC32
)

set "FileSize="
echo %~nx0: Failed to delete file %CD%\all_checksums.txt
echo.
type "%TEMP%\DelErrorMessage.tmp"
del "%TEMP%\DelErrorMessage.tmp"
echo.
echo Is this file opened in an application?
echo.
set "Retry=N"
set /P "Retry=Retry (N/Y)? "
if /I "%Retry%" == "Y" (
    set "Retry="
    cls
    goto CheckOutput
)
set "Retry="
goto :EOF

:CheckOutput
if exist "all_checksums.txt" goto DeleteOutput

:CalcCRC32
for /R %%F in (*.txt) do (
    if /I not "%%F" == "%CD%\all_checksums.txt" (
        crc32sum.exe "%%F" >>"all_checksums.txt"
    )
)

The output file in current directory is deleted if already existing from a previous run. Extra code is added to verify if deletion was successful and informing the user about a failed deletion with giving the user the possibility to retry after closing the file in an application if that is the reason why deletion failed.

The FOR command searches because of option /R recursive in current directory and all its subdirectories for files with extension txt. The name of each found file with full path always without double quotes is hold in loop variable F for any text file found in current directory or any subdirectory.

The CRC32 sum is calculated by 32-bit console application crc32sum in current directory for all text files found with the exception of the output file all_checksums.txt in current directory. The output of this small application is redirected into file all_checksums.txt with appending the single output line to this file.

It is necessary to enclose the file name with path in double quotes because even with no *.txt file containing a space character or one of the special characters &()[]{}^=;!'+,`~ in its name, the path of the file could contain a space or one of those characters.

For the files

C:\Temp\test 1.txt
C:\Temp\test 2.txt
C:\Temp\test_3.txt
C:\Temp\TEST\123-9.txt
C:\Temp\TEST\abc.txt
C:\Temp\TEST\hello.txt
C:\Temp\TEST\hellon.txt
C:\Temp\Test x\test4.txt
C:\Temp\Test x\test5.txt

the file C:\Temp\all_checksums.txt contains after batch execution:

f44271ac *test 1.txt
624cbdf9 *test 2.txt
7ce469eb *test_3.txt
cbf43926 *123-9.txt
352441c2 *abc.txt
0d4a1185 *hello.txt
38e6c41a *hellon.txt
1b4289fa *test4.txt
f44271ac *test5.txt

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cls /?
  • del /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • type /?

One of the help pages output on running for /? informs about %~I, %~fI, %~dI, %~pI, %~nI, %~xI, %~sI, %~aI, %~tI, %~zI.

Using in a batch file f (in lower case) as loop variable and referencing it with %%~f is a syntax error as command processor expects next the loop variable. %%~ff would be right, but could be different to %%~fI (name of a file/folder with full path and extension without quotes) in comparison to %%~I (string without surrounding quotes).

It is not advisable to use (those) small letters as loop variable. It is better to use upper case letters or character # as loop variable. The loop variable and also those modifiers are case sensitive while nearly everything else in a batch file is case insensitive.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thank you so much, this code is very nice. I can get mine to work by putting quotes around the last %%f, but I will probably use yours as it is much nicer. – Mishca de Costa Dec 09 '15 at 14:35