Solved

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

Posted on 2016-11-25
10
22 Views
Last Modified: 2016-11-30
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
Comment
Question by:Dion Christensen
10 Comments
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
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
 
LVL 9

Expert Comment

by:Tomas Valenta
Comment Utility
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
 
LVL 6

Expert Comment

by:Andy
Comment Utility
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
 

Author Comment

by:Dion Christensen
Comment Utility
@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
 
LVL 6

Accepted Solution

by:
Andy earned 500 total points
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Dion Christensen
Comment Utility
@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
 
LVL 6

Assisted Solution

by:Andy
Andy earned 500 total points
Comment Utility
OK try this:

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

(e.g. -4 for .pdf -5 for .xlsm)
0
 

Author Comment

by:Dion Christensen
Comment Utility
@Andy
Amazing... Thank you
0
 
LVL 6

Expert Comment

by:Andy
Comment Utility
My pleasure! @Dion
0
 

Author Closing Comment

by:Dion Christensen
Comment Utility
Simple amazing help from @Andy
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now