我刚刚制作的文本文件的“访问被拒绝”错误消息?

时间:2010-12-02 21:06:58

标签: batch-file cmd access-denied

大约3天前我问了一个问题,可以在这里找到:

how to replace a string on the second line in a text file using a batch file?

我正在将文本文件中的字母转换为各自的数字。我收到错误消息,例如“拒绝访问”和“无法找到此文件” - 但是同样的批处理文件给了我所有这些错误也是使这些文本文件开始的文件!所以它应该与批处理文件本身在同一目录中(除非另有说明),对吧?我甚至去了那个文件夹并检查过,他们就在那里。

我确实添加了一个小脚本,以便在创建文件后隐藏文件,这样它就不会显得那么杂乱。我是通过使用

完成的
attrib +h C:\script\%name%.txt

使用此命令隐藏文件会使正在搜索/调用它的批处理程序不可见吗?

以下是该文件的链接“stringparsing.bat”:http://uploading.com/files/a1m1d2f4/stringparsing.bat/

如果你可以协助我让这个程序执行任务而不会出现任何错误,我们将不胜感激!

这是完整的“stringparsing.bat”文件:

@echo off
setlocal enabledelayedexpansion 
title BETA
cls
cd C:\script\st
echo.
echo.
echo.
echo      Setting Variables...
echo      Loading Language Database...

:: ###################################################################################
::    CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE
:: ###################################################################################




TIMEOUT /t 5 /nobreak > nul

goto MAIN

:MAIN
set foo=0
cls
echo.
echo.
echo.
echo.
echo            ===================================
echo             #################################        
echo             #######     Main Menu:    #######
echo             #################################
echo            ===================================
echo.
echo.
echo             1.) Create New Language File...
echo.
echo             2.) Load Existing Lanuage File...
echo.
echo             3.) Settings...

echo ---------------------------------------------------------

SET /p CHOICE= Select a Function:
IF %CHOICE%== 1 GOTO CREATE
IF %CHOICE%== 2 GOTO LOAD
IF %CHOICE%== 3 GOTO SETTINGS

GOTO MAIN

:CREATE
cls
title Step 1
echo.
echo.
echo.
echo         =================================================================================
echo.
set /p name=  please type a name for your new language file:
echo.
echo         =================================================================================
cls
echo. > %name%.txt
echo.
echo.
echo.
echo        ==============================================================
echo        ##############################################################
echo        #============================================================#
echo        #                                                            #
echo        # - After you hit enter you will be redirected               #
echo        #   to a Live Typer. so anything you type into               #
echo        #   it will be sent to %name%.txt.                           #
echo        #                                                            #
echo        #                                                            #
echo        # - Next, select load language File For Encoding!            #
echo        #                                                            #
echo        #============================================================#
echo        ##############################################################
echo        ==============================================================

set /p line1= :
echo %line1% >> %name%.txt 2> nul


echo %name% > Language_File.txt
attrib +h Language_File.txt
set /a foo+ =1
)
echo.
echo ==========================================================

goto LOAD



:LOAD
set /a foo+ =1
IF %foo%== 2 goto loadexternal
goto LOAD23

:loadexternal
echo.
echo language file is loading now!
set /p name=<Language_File.txt
timeout /t 4 /nobreak > nul
echo.
echo.
echo Language_File Loaded!
pause >nul
goto LOAD23

:LOAD23
cls
echo.
echo.
echo.
echo.
echo.
echo        Encoding Your Language File... Please Wait... 
echo.
echo.
echo.

for /f "delims=" %%i in (!name!.txt) do ( 
echo translating "%%i"
set var=%%i 
set var=!var:a=1 ! 
set var=!var:b=2 ! 
set var=!var:c=3 ! 
set var=!var:d=4 ! 
set var=!var:e=5 ! 
set var=!var:f=6 ! 
set var=!var:g=7 ! 
set var=!var:h=8 ! 
set var=!var:i=9 ! 
set var=!var:j=10 ! 
set var=!var:k=11 ! 
set var=!var:l=12 ! 
set var=!var:m=13 ! 
set var=!var:n=14 ! 
set var=!var:o=15 ! 
set var=!var:p=16 ! 
set var=!var:q=17 ! 
set var=!var:r=18 ! 
set var=!var:s=19 ! 
set var=!var:t=20 ! 
set var=!var:u=21 ! 
set var=!var:v=22 ! 
set var=!var:w=23 ! 
set var=!var:x=24 ! 
set var=!var:y=25 ! 
set var=!var:z=26 ! 

echo !var! 
) 

echo !var! > !name!.txt

pause >nul
TIMEOUT /t 5 /nobreak > nul

goto MAIN


:END
cls
title SHUTTING DOWN...
echo.
echo.
echo.
echo            Terminating service stream...
echo.
echo.
echo.
echo.
echo            Done! Thank you for using this program!
TIMEOUT /t 5 /nobreak > nul





::(%xx%) -1  I/O Stream= "SHELL.dll" 
:: IF EXIST [&1[Parser_2009]] exit

Exit


:: #####################################################################################

2 个答案:

答案 0 :(得分:1)

你遇到了一些问题。首先,访问被拒绝的问题是您重定向到隐藏文件。

echo %name% > Language_File.txt
attrib +h Language_File.txt

请注意,第一次运行脚本时,它将起作用,因为Language_File.txt将不存在,因此不会被隐藏。第二次运行它时,您将被拒绝访问。我不知道Windows为什么不让你这样做。您可以通过几种方式解决此问题。

1. 将文件保存到用户的临时目录。使用这种方法,您的目录不会混乱。

echo %name% > %TMP%\Language_File.txt

2. 将您的文件保存到您拥有的子目录中,这样就不会使脚本的目录混乱。

if not exist workspace mkdir workspace
echo %name% > workspace\Language_File.txt

3. 在使用之前取消隐藏文件。由于该文件可能在您第一次运行脚本时不存在,因此,如果它存在,您可能只应该attrib -h

if exist Language_File.txt attrib -h Language_File.txt
echo %name% > %TMP%\Language_File.txt
attrib +h Language_File.txt

4. 完全不要使用Language_File.txt!我不明白你为什么需要它。只需使用变量来保存语言文件的名称。事实上,您已经在%name%中拥有该名称,对吗?

其次,您应该检查变量的值以查看它们的确实含义。当您将Language_File.txt的内容加载到变量中时,它会加载所有内容。这包括隐藏的换行符\r\n,尽管脚本似乎将它们作为空格带入变量。参见:

c:\batch\t>echo language file is loading now!
language file is loading now
C:\batch\t>set /p name= <Language_File.txt
C:\batch\t>echo -%name%-
-langfile  -

如果你回显用连字符包围的%name%,你可以看到它(可能是)换行符后面有2个空格。要解决此问题,可以使用set来修剪尾随字符。

C:\batch\t>echo language file is loading now!
language file is loading now
C:\batch\t>set /p name= <Language_File.txt
C:\batch\t>set name=%name:~0,-2%
C:\batch\t>echo -%name%-
-langfile-

在第二个例子中,`%name%没有隐藏的字符。

最后,您只需使用!来访问在for循环中设置的变量。因此,对!name!的所有引用都应为%name%。这可能是你的“无法找到文件”错误。

答案 1 :(得分:0)

继承人“stringparsing.bat”文件:

@echo off
setlocal enabledelayedexpansion 
title BETA
cls
cd C:\script\st
echo.
echo.
echo.
echo      Setting Variables...
echo      Loading Language Database...

:: ###################################################################################
::    CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE
:: ###################################################################################




TIMEOUT /t 5 /nobreak > nul

goto MAIN

:MAIN
set foo=0
cls
echo.
echo.
echo.
echo.
echo            ===================================
echo             #################################        
echo             #######     Main Menu:    #######
echo             #################################
echo            ===================================
echo.
echo.
echo             1.) Create New Language File...
echo.
echo             2.) Load Existing Lanuage File...
echo.
echo             3.) Settings...

echo ---------------------------------------------------------

SET /p CHOICE= Select a Function:
IF %CHOICE%== 1 GOTO CREATE
IF %CHOICE%== 2 GOTO LOAD
IF %CHOICE%== 3 GOTO SETTINGS

GOTO MAIN

:CREATE
cls
title Step 1
echo.
echo.
echo.
echo         =================================================================================
echo.
set /p name=  please type a name for your new language file:
echo.
echo         =================================================================================
cls
echo. > %name%.txt
echo.
echo.
echo.
echo        ==============================================================
echo        ##############################################################
echo        #============================================================#
echo        #                                                            #
echo        # - After you hit enter you will be redirected               #
echo        #   to a Live Typer. so anything you type into               #
echo        #   it will be sent to %name%.txt.                           #
echo        #                                                            #
echo        #                                                            #
echo        # - Next, select load language File For Encoding!            #
echo        #                                                            #
echo        #============================================================#
echo        ##############################################################
echo        ==============================================================

set /p line1= :
echo %line1% >> %name%.txt 2> nul


echo %name% > Language_File.txt
attrib +h Language_File.txt
set /a foo+ =1
)
echo.
echo ==========================================================

goto LOAD



:LOAD
set /a foo+ =1
IF %foo%== 2 goto loadexternal
goto LOAD23

:loadexternal
echo.
echo language file is loading now!
set /p name=<Language_File.txt
timeout /t 4 /nobreak > nul
echo.
echo.
echo Language_File Loaded!
pause >nul
goto LOAD23

:LOAD23
cls
echo.
echo.
echo.
echo.
echo.
echo        Encoding Your Language File... Please Wait... 
echo.
echo.
echo.

for /f "delims=" %%i in (!name!.txt) do ( 
echo translating "%%i"
set var=%%i 
set var=!var:a=1 ! 
set var=!var:b=2 ! 
set var=!var:c=3 ! 
set var=!var:d=4 ! 
set var=!var:e=5 ! 
set var=!var:f=6 ! 
set var=!var:g=7 ! 
set var=!var:h=8 ! 
set var=!var:i=9 ! 
set var=!var:j=10 ! 
set var=!var:k=11 ! 
set var=!var:l=12 ! 
set var=!var:m=13 ! 
set var=!var:n=14 ! 
set var=!var:o=15 ! 
set var=!var:p=16 ! 
set var=!var:q=17 ! 
set var=!var:r=18 ! 
set var=!var:s=19 ! 
set var=!var:t=20 ! 
set var=!var:u=21 ! 
set var=!var:v=22 ! 
set var=!var:w=23 ! 
set var=!var:x=24 ! 
set var=!var:y=25 ! 
set var=!var:z=26 ! 

echo !var! 
) 

echo !var! > !name!.txt

pause >nul
TIMEOUT /t 5 /nobreak > nul

goto MAIN


:END
cls
title SHUTTING DOWN...
echo.
echo.
echo.
echo            Terminating service stream...
echo.
echo.
echo.
echo.
echo            Done! Thank you for using this program!
TIMEOUT /t 5 /nobreak > nul





::(%xx%) -1  I/O Stream= "SHELL.dll" 
:: IF EXIST [&1[Parser_2009]] exit

Exit


:: #####################################################################################

我终于学会了如何格式化代码片段。 (如果你需要,可以链接到另一个副本。)

how to replace a string on the second line in a text file using a batch file?