if exist rm file not working in batch script

Advertisement

ksrinivasan
Guest

if exist rm file not working in batch script

This command works in my Windows batch script
rm "/rootfolder/subfolder/filename.csv"
however, the following does not work
if exist "/rootfolder/subfolder/filename.csv"
rm "/rootfolder/subfolder/filename.csv"
Can someone suggest how I can correct it.

Reply with quote

Advertisement

ksrinivasan
Guest

if exist rm file not working in batch script

Here is the actual solution
@echo off
setlocal
 
REM Specify the path to WinSCP executable
set WINSCP_PATH="C:\Program Files (x86)\WinSCP\WinSCP.com"
 
REM Specify the path to the WinSCP script file
set SCRIPT_PATH="C:\path\to\winscp_script.txt"
 
REM Specify the path for the log file
set LOG_PATH="C:\path\to\winscp.log"
 
REM Create a temporary script file
set TEMP_SCRIPT="C:\path\to\temp_script.txt"
 
REM Connect to the server and list .csv files
echo open sftp://username:password@example.com/ -hostkey="ssh-rsa 2048 xxxxxxxxxxxxxxx..." > %TEMP_SCRIPT%
echo cd /remote/directory/path >> %TEMP_SCRIPT%
echo ls *.csv >> %TEMP_SCRIPT%
echo close >> %TEMP_SCRIPT%
echo exit >> %TEMP_SCRIPT%
 
REM Run the temporary script and capture the output
%WINSCP_PATH% /script=%TEMP_SCRIPT% /log=%LOG_PATH% /loglevel=2 > output.txt
 
REM Check if any .csv files exist based on the output
findstr ".csv" output.txt >nul
if %errorlevel%==0 (
    REM If .csv files exist, create a script to delete them
    echo open sftp://username:password@example.com/ -hostkey="ssh-rsa 2048 xxxxxxxxxxxxxxx..." > %SCRIPT_PATH%
    echo cd /remote/directory/path >> %SCRIPT_PATH%
    echo rm *.csv >> %SCRIPT_PATH%
    echo close >> %SCRIPT_PATH%
    echo exit >> %SCRIPT_PATH%
 
    REM Execute the script to delete the files
    %WINSCP_PATH% /script=%SCRIPT_PATH% /log=%LOG_PATH% /loglevel=2
) else (
    echo No CSV files found to delete
)
 
REM Clean up
del %TEMP_SCRIPT%
del output.txt
 
endlocal

Reply with quote

Advertisement

You can post new topics in this forum