0

I have this code, this always returns True and file get moved. What is the problem with my code ? I am running on Windows batch file.

     @echo off
     title echo
     setlocal enableDelayedExpansion
     for /F "tokens=* delims=" %%a in (vendor.txt) do (
     CD /D H:\RCP\inbound\vendor\drop\%%a\
     for /f "delims=" %%1 in ('dir *.xlsx /b') do (
     set  filename=%%~n1
    set text=!filename!_Response_File.xlsx
    set  filepath=H:\RCP\outbound\vendor\%%a\!text! 
    echo !text!  
    echo !filepath!     
    echo H:\RCP\outbound\vendor\%%a\!text!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
    if exist  H:\RCP\outbound\vendor\%%a\!text! (
    echo file will be moved
)  else (
   echo file does not exist
)
)
)
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Are you using variables in the file name? `%a%` or `%text%` are environment variables. In this case, you need to write `H:\RCP\outbound\vendor%a%%text% ` otherwise they aren't resolved correctly. Try to print them on screen via `ECHO %a%, %text%` to see if their values are correct. If you're not using variables, check [this](https://stackoverflow.com/questions/6828751/batch-character-escaping) for correctly escaping the `%` character in the filename. – Matt Apr 27 '23 at 06:31
  • 1
    I guess this line of code is within a `for` loop? We need to see the full loop (plus any used variables that are defined outside the loop) – Stephan Apr 27 '23 at 06:36

1 Answers1

0

It might simply be a typo (%%a instead of %a%) but why not use more meaningful variable names e.g.:

set folder=yourfolder
set filename=yourfile.txt

if exist H:\RCP\outbound\vendor\%folder%\%filename% (
   echo file will be moved: %folder%\%filename%
) else (
   echo file does not exist: %folder%\%filename%
)
Paul Maxwell
  • 33,002
  • 3
  • 32
  • 51
  • I tried this solution,however it does not work. All files still get moved, if exist gives True everytime. Please help further – abhishek kalwit Apr 27 '23 at 08:29
  • if exist H:\RCP\outbound\vendor\%a%\%text% ( echo file will be moved ) else ( echo file does not exist ) – abhishek kalwit Apr 27 '23 at 08:29
  • If the problem isn't solved by fixing the typo, the problem must be elsewhere. Why not edit your question and include the complete script (sanitize anything that needs it), you also need to demonstrate that some files you are testing against do actually exist (and that some do not - i.e. that you are testing for both conditions) – Paul Maxwell Apr 27 '23 at 08:36
  • Here is the complete script... – abhishek kalwit Apr 27 '23 at 08:44
  • all echo statement print right values but if exist always return True even if response file does not exist in outbound folder – abhishek kalwit Apr 27 '23 at 09:13
  • Don't add it as a comment. EDIT your question there is an "edit" link under the question, so that others can help answer as it is now late evening for me. – Paul Maxwell Apr 27 '23 at 09:29
  • 1
    I have updated original question with script – abhishek kalwit Apr 27 '23 at 09:55