Solved 'if exist' batch condition

I'm trying to write a batch script that will check to see if a file exists and then delete it if it does, otherwise display a message.
Code:
@echo off

echo This script will del 'results.txt' if it exists
echo.
if exist C:\Users\Sam\Desktop\results.txt (
    del C:\Users\Sam\Desktop\results.txt
    pause
) else (
    echo file not found
    pause
)
I've seen this kind of syntax on a few tutorial pages, but no matter how I type it, and regardless of whether the file exists or not, the script returns 'file not found'.

I've tried running the script from the same directory as the file I want it to delete and just typing the file name without location, and it still doesn't work. I've even tried wrapping the location is quotes which doesn't work either.

Is this still supported in Vista or am I typing it wrong?
Thanks.
 

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5630
    CPU
    Intel Core 2 Duo T5500 1.66GHz 65nm 667MHz FSB 2MB L2
    Motherboard
    Acer Grapevine Intel i945GM Chipset
    Memory
    2x1GB DDR2 333MHz
    Graphics Card(s)
    Intel Integrated GMA 950 256MB
    Sound Card
    Realtek High Definition Audio
    Monitor(s) Displays
    15.4" WXGA Acer CrystalBrite LCD 16ms
    Screen Resolution
    1280x800
    Hard Drives
    200GB 5400rpm Toshiba MK2035GSS ATA-7, 250GB 7200rpm Maxtor External USB Hard Drive
Try this

Code:
@echo off
echo This script will del 'results.txt' if it exists
echo.
if exist C:\Users\Sam\Desktop\results.txt (del C:\Users\Sam\Desktop\results.txt) else (echo file not found)
Pause
 

My Computers

System One System Two

  • Operating System
    Windows 11 Workstation
    Manufacturer/Model
    doofenshmirtz evil incorporated
    CPU
    Ryzen 9 5950X
    Motherboard
    Asus ROG Crosshair VIII Formula
    Memory
    Corsair Vengeance RGB PRO Black 64GB (4x16GB) 3600MHz AMD Ryzen Tuned DDR4
    Graphics Card(s)
    ASUS AMD Radeon RX 6900 XT 16GB ROG Strix LC OC
    Sound Card
    Creative
    Monitor(s) Displays
    3 x27" Dell U2724D & 1 x 34" Dell U3415W
    Hard Drives
    Samsung 980 Pro 1TB M.2 2280 PCI-e 4.0 x4 NVMe Solid State
    Drive
    PSU
    1500W ThermalTake Toughpower
    Case
    ThermalTake Level 10 GT
    Cooling
    Enermax Liqtech 240
    Keyboard
    Surface Ergonomic.
    Mouse
    Logitech Performance MX
    Internet Speed
    350 Mb/s
    Other Info
    WinTV NovaTD
    HP CP1515n Color Laser
    Sony BD-5300S-0B Blu-ray Writer
    Microsoft LifeCam Cinema
    APC 750i Smart UPS
  • Operating System
    windows 10
    Manufacturer/Model
    Surface Pro 3
    CPU
    1.9GHz Intel Core i5-4300U (dual-core, 3MB cache, up to 2.9GHz with Turbo Boost)
    Memory
    4GB
    Graphics card(s)
    Intel HD Graphics 4400
    Monitor(s) Displays
    12" Multi Touch
    Screen Resolution
    2160 x 144
    Hard Drives
    128GB
    Mouse
    Logitech
    Keyboard
    yes
    Internet Speed
    350 Mb/s
I get the same error message as my script.
 

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5630
    CPU
    Intel Core 2 Duo T5500 1.66GHz 65nm 667MHz FSB 2MB L2
    Motherboard
    Acer Grapevine Intel i945GM Chipset
    Memory
    2x1GB DDR2 333MHz
    Graphics Card(s)
    Intel Integrated GMA 950 256MB
    Sound Card
    Realtek High Definition Audio
    Monitor(s) Displays
    15.4" WXGA Acer CrystalBrite LCD 16ms
    Screen Resolution
    1280x800
    Hard Drives
    200GB 5400rpm Toshiba MK2035GSS ATA-7, 250GB 7200rpm Maxtor External USB Hard Drive
Strange as it works fine for me.

I've uploaded the bat I used for the test, see if that works for you as the forum could be adding spaces with the copy and paste.
 

Attachments

  • test.bat
    200 bytes · Views: 1,169

My Computers

System One System Two

  • Operating System
    Windows 11 Workstation
    Manufacturer/Model
    doofenshmirtz evil incorporated
    CPU
    Ryzen 9 5950X
    Motherboard
    Asus ROG Crosshair VIII Formula
    Memory
    Corsair Vengeance RGB PRO Black 64GB (4x16GB) 3600MHz AMD Ryzen Tuned DDR4
    Graphics Card(s)
    ASUS AMD Radeon RX 6900 XT 16GB ROG Strix LC OC
    Sound Card
    Creative
    Monitor(s) Displays
    3 x27" Dell U2724D & 1 x 34" Dell U3415W
    Hard Drives
    Samsung 980 Pro 1TB M.2 2280 PCI-e 4.0 x4 NVMe Solid State
    Drive
    PSU
    1500W ThermalTake Toughpower
    Case
    ThermalTake Level 10 GT
    Cooling
    Enermax Liqtech 240
    Keyboard
    Surface Ergonomic.
    Mouse
    Logitech Performance MX
    Internet Speed
    350 Mb/s
    Other Info
    WinTV NovaTD
    HP CP1515n Color Laser
    Sony BD-5300S-0B Blu-ray Writer
    Microsoft LifeCam Cinema
    APC 750i Smart UPS
  • Operating System
    windows 10
    Manufacturer/Model
    Surface Pro 3
    CPU
    1.9GHz Intel Core i5-4300U (dual-core, 3MB cache, up to 2.9GHz with Turbo Boost)
    Memory
    4GB
    Graphics card(s)
    Intel HD Graphics 4400
    Monitor(s) Displays
    12" Multi Touch
    Screen Resolution
    2160 x 144
    Hard Drives
    128GB
    Mouse
    Logitech
    Keyboard
    yes
    Internet Speed
    350 Mb/s
Oh I just realized what was wrong. When I saved the 'results.txt' file through Wing, I had already selected 'Text File' for the file extension and had typed the file name as 'results.txt' therefore the file became: 'results.txt.txt'.

Both our scripts work now since the file extension has been fixed. Thanks for your effort though; I had thought that my syntax was incorrect.
 

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5630
    CPU
    Intel Core 2 Duo T5500 1.66GHz 65nm 667MHz FSB 2MB L2
    Motherboard
    Acer Grapevine Intel i945GM Chipset
    Memory
    2x1GB DDR2 333MHz
    Graphics Card(s)
    Intel Integrated GMA 950 256MB
    Sound Card
    Realtek High Definition Audio
    Monitor(s) Displays
    15.4" WXGA Acer CrystalBrite LCD 16ms
    Screen Resolution
    1280x800
    Hard Drives
    200GB 5400rpm Toshiba MK2035GSS ATA-7, 250GB 7200rpm Maxtor External USB Hard Drive
Back
Top