China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 17:56
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Original] Converting Chinese resource files for Struts and JSF framework internationalization View 1,114 Replies 2
Original Poster Posted 2006-06-07 20:07 ·  中国 辽宁 大连 电信
初级用户
Credits 110
Posts 27
Joined 2006-06-03 17:25
20-year member
UID 56491
Status Offline
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.


@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
Floor 2 Posted 2006-06-14 18:21 ·  中国 山西 太原 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re wentimao:

Very sorry! This original topic by you was overlooked for so long! I have now done a little cleanup on the topic, changed the title, highlighted it with red underlining, and awarded 16 credits.

As for checking the existence of a file and folder with the same name, I still hold the same view as in related reply [2] above: don't bother with it unless there is really a need to bother with it.

As for the later for/f solution I proposed, it is roughly as you said: for /f "usebackq" requires that ("") must contain a filename, otherwise it will be unable to read its text contents and will display an error message, while the following )2>nul can suppress the error message.

As for the fact that this solution has some flaws, one known issue is that if the operation after confirming file existence is placed after do, it may be executed multiple times because the file contains multiple lines of text. The improved solution is as follows:

for /f "usebackq" %%f in ("%~f1") do set) >nul 2>nul || echo File %~f1 does not exist

Re Others:

What feels rather strange is that, whether %~f1 exists or not, for /f does not modify %errorlevel%, but the following && or || still work normally. Does this mean that the conditions for && and || do not depend on %errorlevel%?
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2006-06-15 07:54 ·  中国 辽宁 大连 电信
初级用户
Credits 110
Posts 27
Joined 2006-06-03 17:25
20-year member
UID 56491
Status Offline
I modified that search code using one person's method. This way there is no need to create a temporary file, which feels much more convenient. 


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 (
(for /f "delims=" %%p in ('dir /a /b /s %%i:\native2ascii.exe') do (
echo Found %%p
set path=%path%;%%~dpp&&goto :pass
)) 2>nul
)
echo.
echo native2ascii.exe does not exist on your computer, conversion cannot be completed.
echo.
goto :end
xp,cmd
Forum Jump: