Over the past few days I posted on the forum to ask questions, and based on everyone's help I made this batch file. It is mainly used for converting Chinese resource files for Struts and JSF framework internationalization. Please take a look and tell me what still needs to be corrected or optimized. Also, please don't keep replying in my earlier threads anymore.
willsort, look at these lines:
if exist "%~f1\" goto error
if not exist "%~f1" goto error
Originally this was to prevent the case where the input parameter is a folder, but like I said, if in the current folder there is both a file named aa and a folder named aa, then won't these two lines fail?
The method you suggested was:
for /f "usebackq" in ("%~f1") do ...)2>nul
What does this method mean? Does it try to operate on it no matter whether it is a file or a directory, and if it's wrong then just suppress it? Is that what it means? I don't quite understand, and hope you can explain it.
The discussions related to this topic are as follows:
How to get the full path of a file?
http://www.cn-dos.net/forum/viewthread.php?tid=20996
How to determine whether a parameter is a file or a directory
http://www.cn-dos.net/forum/viewthread.php?tid=20999
How to read the paths in the PATH environment variable one by one?
http://www.cn-dos.net/forum/viewthread.php?tid=21008
How to search the whole disk for a file and add its path to the environment variable
http://www.cn-dos.net/forum/viewthread.php?tid=21013
How to change the value of %1 from the command line in a batch file?
http://www.cn-dos.net/forum/viewthread.php?tid=21014
How not to display an error prompt when dir does not find the file
http://www.cn-dos.net/forum/viewthread.php?tid=21021
[ Last edited by willsort on 2006-6-14 at 17:45 ]
willsort, look at these lines:
if exist "%~f1\" goto error
if not exist "%~f1" goto error
Originally this was to prevent the case where the input parameter is a folder, but like I said, if in the current folder there is both a file named aa and a folder named aa, then won't these two lines fail?
The method you suggested was:
for /f "usebackq" in ("%~f1") do ...)2>nul
What does this method mean? Does it try to operate on it no matter whether it is a file or a directory, and if it's wrong then just suppress it? Is that what it means? I don't quite understand, and hope you can explain it.
@echo off
cls
echo.
echo.
echo Chinese resource conversion batch file
echo =====================================================
echo.Usage instructions:
echo.
echo ⒈You can directly copy the source file to be converted and then right-click
echo paste it onto the batch file. The generated ApplicationResources_zh_CN.properties
echo and messages_
echo zh_CN.properties files will automatically be saved in the same directory as the source file.
echo.
echo ⒉Run the batch file, then enter the full path of the source file. If the source file
echo is in the same directory as the batch file, then you only need to type the source filename
echo to complete the conversion as well.
echo .
echo ⒊Under DOS, enter the batch file name, followed by the path and filename of the source file,
echo and the conversion will also be completed. Likewise, if they are in the same directory,
echo you do not need to type the path.
echo.
echo ⒋When native2ascii.exe has not been set in path, it will automatically search
echo all local drives and add it to path. Completely intelligent.
echo.
echo It is recommended to place this batch file on the desktop, then use the first method to generate.
echo.=====================================================
:start
if "%~1"=="exit" goto end
if exist "%~dp0temp.sbl" del/f "%~dp0temp.sbl"
if exist "%~f1\" goto error
if not exist "%~f1" goto error
set x="sbl"
for %%i in (native2ascii.exe) do if exist %%~$PATH:i set x=%%~$PATH:i
if not %x% == "sbl" goto pass
echo.
echo The path variable does not include the path to native2ascii.exe. This program will now automatically search all drives
echo for the directory containing native2ascii. This process may take some time, please wait....
echo.
echo.
echo Searching......
echo.
echo.
for %%i in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
cd %%i:\
dir /a /b /s %%i:native2ascii.exe>%~dp0temp.sbl 2>nul && goto add
)
echo.
echo native2ascii.exe does not exist on your computer, conversion cannot be completed.
echo.
goto end
:add
for /f "tokens=*" %%i in (%~dp0temp.sbl) do (
path=%path%;%%~dpi
echo Found %%i
)
del %~dp0temp.sbl>nul 2>nul
:pass
%~d1
cd %~p1
native2ascii -encoding gb2312 "%~f1" ApplicationResources_zh_CN.properties
copy/y ApplicationResources_zh_CN.properties messages_zh_CN.properties
echo.
echo Conversion successful!!!
echo.
goto end
:error
echo.
echo.
echo You did not enter the filename to be converted, or the filename does not exist!
echo.
set /p file=Please enter the filename to be converted (enter exit to quit):
call :start %file%
:end
@echo on
The discussions related to this topic are as follows:
How to get the full path of a file?
http://www.cn-dos.net/forum/viewthread.php?tid=20996
How to determine whether a parameter is a file or a directory
http://www.cn-dos.net/forum/viewthread.php?tid=20999
How to read the paths in the PATH environment variable one by one?
http://www.cn-dos.net/forum/viewthread.php?tid=21008
How to search the whole disk for a file and add its path to the environment variable
http://www.cn-dos.net/forum/viewthread.php?tid=21013
How to change the value of %1 from the command line in a batch file?
http://www.cn-dos.net/forum/viewthread.php?tid=21014
How not to display an error prompt when dir does not find the file
http://www.cn-dos.net/forum/viewthread.php?tid=21021
[ Last edited by willsort on 2006-6-14 at 17:45 ]
xp,cmd
