Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 811
  • Last Modified:

Make batch file check if file is locked, and open file if not.

Hello Experts
I have this little batch file, and right now it can check if the file is already open and tell you that it is open and you need to try again later.
The thing i need help with, is expanding it, so that if the ren command is successful I need the batch file to open the pdf file.

Original code that works:
@echo off
ren "Test.pdf" "Test.pdf" 2>nul || (
  echo Sorry, but the file is in use.
  echo Please try again later.
)
pause

Open in new window


My take on open if successful - that don't work and I need help with getting to work:
@echo off
ren "Test.pdf" "Test.pdf" 2>nul || (
  echo Sorry, but the file is in use.
  echo Please try again later.
) else (
start "C:\Test\" Test.pdf
pause

Open in new window


Do one of you know how to get it working :) ??
0
Dion Christensen
Asked:
Dion Christensen
2 Solutions
 
oBdACommented:
You're missing the closing round bracket.
Try it like this:
@echo off
ren "Test.pdf" "Test.pdf" 2>nul || (
	echo Sorry, but the file is in use.
	echo Please try again later.
) else (
	start "" "C:\Test\Test.pdf"
)
pause

Open in new window

0
 
Tomas ValentaIT ManagerCommented:
try this one:
  1. @echo off
  2. ren "Test.pdf" "Test.pdf" 2>nul || (
  3.  echo Sorry, but the file is in use.
  4.  echo Please try again later.
  5. ) else (
  6. start C:\Test\Test.pdf
  7. )
  8. pause
0
 
AndyIt ConsultantCommented:
Try this script:
@echo off
cd /d "c:\test"

for %%a in (test.xlsx) do call :next "test.xlsx"
echo done
pause
goto :eof

:next
set "filename=%~1"
2>nul (call;>>"%filename%") && (
  start C:\test\test.xlsx
  goto done
) || (
  echo "%filename%" is in use or is read only!
)
timeout /t 1 /nobreak
:done

Open in new window

0
Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

 
Dion ChristensenTechnical DesignerAuthor Commented:
@Andy
That works like a dream.. but it failes when I have "spaces" in my file name like "This is a good day.pdf"
Do you have a fix for that ??
0
 
AndyIt ConsultantCommented:
Try this one:
@echo off
cd /d "c:\test"

set "filename="test 1.xlsx""
2>nul (call;>>"%filename%") && (
  start %filename%
  goto done
) || (
  echo "%filename%" is in use or is read only!
pause
goto done
)
timeout /t 1 /nobreak
:done
exit

Open in new window

0
 
Dion ChristensenTechnical DesignerAuthor Commented:
@Andy
Thanks for all the help.
The file looks like this now:
@echo off
:: Info
cd /d "C:\Test"

:: Info
for /f "delims=" %%a in ("Test 1.pdf") do call :next "Test 1.pdf"
echo done
pause
goto :eof

:: Info
:next
set "filename=%~1"
2>nul (call;>>"%filename%") && (
  start C:\Test\"%filename%"
) || (
  echo %filename% is in use or is read only!
timeout /t 10
)
exit

Open in new window

This all works, but there is one small thing that i need help changing now.
I want the echo command to take %filename% and remove the file extension if that is posible.
Can this be done ??
0
 
AndyIt ConsultantCommented:
OK try this:

echo %filename:~0,-4% is in use or is read only!

(e.g. -4 for .pdf -5 for .xlsm)
0
 
Dion ChristensenTechnical DesignerAuthor Commented:
@Andy
Amazing... Thank you
0
 
AndyIt ConsultantCommented:
My pleasure! @Dion
0
 
Dion ChristensenTechnical DesignerAuthor Commented:
Simple amazing help from @Andy
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now