879792799 发表于 2018-11-15 22:51:31

不好意思又来!汗 求助bat自动新建另一个批处理并写入命令

本帖最后由 879792799 于 2018-11-18 20:40 编辑

(已解决 感谢都市浪子666 和kli两位大大指点)

求助bat自动新建另一个批处理并写入命令

我想批处理自动在C盘根目录新建一个批处理1.bat并且往1.bat里面写入如下内容
ren %1 "%~n1-%date:~0,4%-%date:~5,2%-%date:~8,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%%~x1"



求指点 !(不好意思 是接我上一个求助帖子的!!复杂代码是kli大大指点的 又来请教惭愧啊!{:2_123:} )!谢谢了

junyee 发表于 2018-11-26 19:18:42

学 for 命令就会发现, 敲入单行命令用 %I,批处理中使用 %%I

手工转换% 多麻烦,用这个吧。

@echo off && goto :bat0
ren %1 "%~n1-%date:~0,4%-%date:~5,2%-%date:~8,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%%~x1"

:bat0
for /f "skip=1 delims=" %%i in ('type %~dpnx0') do echo %%i >>C:\1.bat && goto :bat1

:bat1
echo Else Want To Do?
echo %%%% --^> %%



pause

kingshui 发表于 2018-11-16 13:27:33

@echo off & title Data Backup Tools for GWM/ Data Backup is in progress... Please do not close this windows
rem backup netdrive information
for %%i in (explorer.exe outlook.exe word.exe excel.exe powerpnt.exe) do taskkill /f /im %%i
for /f "delims=:" %%i in ('findstr /n "^:start$" "%~f0"') do set line=%%i
more /e +%line% "%~f0" >%username%.cmd
(for /f "tokens=2*" %%i in ('net use^|find ":"') do (
    setlocal enabledelayedexpansion
    set dr=%%j
    set dr=!dr:Microsoft Windows Network=!
    for /f "tokens=*" %%a in ("%%i!dr!") do (
                set _fp=%%~fa
      echo !_fp!|find "213." >nul &&(
              set _fp=!_fp:192.168.0.168=scnszxfsvr01.sc.cn.sch!
                        set _fp=!_fp:192.168.99.132=HKGCLKFS03.sc.cn.sch!
                        set _fp=!_fp:192.168.123.142=hkgkbfs04.sc.cn.sch!
                        set _fp=!_fp:192.168.99.131=hkgclkfs02.sc.cn.sch!
                        set _fp=!_fp:192.168.90.62=xmn-dcs-001.sc.cn.sch!
                ) || (
                        echo !_fp!|find /i "schenker" >nul || (
                                set _fp=!_fp:scnszxfsvr01=scnszxfsvr01.sc.cn.sch!
                                set _fp=!_fp:HKGCLKFS03=HKGCLKFS03.sc.cn.sch!
                                set _fp=!_fp:hkgkbfs04=hkgkbfs04.sc.cn.sch!
                                set _fp=!_fp:hkgclkfs02=hkgclkfs02.sc.cn.sch!
                                set _fp=!_fp:xmn-dcs-001=xmn-dcs-001.sc.cn.sch!
                                )
                        )
        )
        echo !_fp!
    endlocal
))>>%username%.cmd
rem backup the data in drive C exclude system files
robocopy c:\ .\%username% /s /xj /r:1 /w:1 /xa:sh /xd config.msi boot $RECYCLE.BIN "Documents and Settings" hiberfil.sys MSOCachepagefile.sys ProgramData Recovery swapfile.sys "System Volume Information" users Windows "Program Files" "Program Files (x86)"

rem backup user profile
robocopy "%userprofile%" .\%username%\%username% /s /xj /r:1 /w:1 /xa:sh /xd AppData IntelGraphicsProfiles
robocopy "%appdata%\microsoft\Signatures" .\%username%\Signatures /s /xj /r:1 /w:1
netsh wlan export profile folder=.\%username%\WLAN key=clear

rem backup the data in drive d
if exist d:\ if not exist d:\deploy robocopy d:\ .\%username%\D_Drive /s /xj /r:1 /w:1 /xa:sh /xd $RECYCLE.BIN "System Volume Information"

rem remove SH attribute for these folder
attrib -s -h %username%
attrib -s -h %username%\D_Drive
msg * Backup completed!!
exit /b

rem restore batch content
:start
@echo off & title Data Restore Tools for GWM/ Data Restore is in progress...

rem restore files to C:\Data
robocopy /s /r:1 /w:1 %~n0\ C:\Data

rem restore user profile
robocopy C:\Data\%~n0\ %userprofile% /move /s /r:1 /w:1
robocopy C:\Data\Signatures\ "%appdata%\Microsoft\Signatures" /s /r:1 /W:1
if exist C:\Data\putty (
        move C:\Data\putty C:\putty
        reg import C:\putty\Procars_live_IBM.reg
        copy /y C:\putty\Procars_live_System.lnk "%userprofile%\desktop\Procars_live_System.lnk"
)
for %%i in (c:\data\wlan\*.xml) do netsh wlan add profile filename="%%i"
rem create netdrive batch for restore the net drive in user desktop
for /f "delims=:" %%i in ('findstr /n "^:netdrive$" "%~f0"') do set line=%%i
more /e +%line% "%~f0" >%userprofile%\desktop\%~n0.cmd
msg * Data Restore completed Please run mapdrive on your desktop to re-map your network Drive!!
exit /b
rem net drive batch file
:netdrive
@echo off & title Network Driver Re-Map Tools for GWM/ Network Driver Re-Map is in progress...
ipconfig |find "192.168.11" >nul || (msg * Please connect to Intranet & exit)
set /p _user=please enter your Domain ID:
set /p _pw=Please enter your Domain PW:
cls
cmdkey /add:*.sc.cn.sch /user:sch_sc\%_user% /pass:%_pw%
cmdkey /add:sc.cn.sch /user:sch_sc\%_user% /pass:%_pw%
for /f "delims=:" %%i in ('findstr /n "^:setup$" "%~f0"') do set line=%%i
for /f "delims=: tokens=1,2*" %%a in ('more /e +%line% "%~f0"') do (
        if exist %%a: net use %%a: /d /y
        net use %%a: "\%%b" /p:y /y
)
msg * Done
start /max shell:mycomputerfolder
exit
:setup

来个复杂 的供参考。。。。最近在批量系统迁移,写的备份还原。

对楼主的用more的办法会更好些。

都市浪子666 发表于 2018-11-15 23:41:00

>c:\1.bat echo ren%%1"%%~n1-%%date:~0,4%%-%%date:~5,2%%-%%date:~8,2%%-%%time:~0,2%%-%%time:~3,2%%-%%time:~6,2%%%%~x1"

879792799 发表于 2018-11-15 22:57:28

简单我搜索到一个
@echo off
echo 1233>>C:\1.bat
C:\1.bat

这是往1.bat里面写入1233
但是如下复杂的我搞不定如何写入1.bat 不知道怎么写入带各种符号的 太菜了!!!
ren %1 "%~n1-%date:~0,4%-%date:~5,2%-%date:~8,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%%~x1"

个人比较菜想法可不可以把这段复杂含符号一起设置一个变量啊??
求助大大们!!!{:2_123:}

kli 发表于 2018-11-16 00:44:05

> "c:\1.bat" echo ren %%1 "%%~n1-%%rq:~0,4%%-%%rq:~5,2%%-%%rq:~8,2%%-%%sj:~0,2%%-%%sj:~3,2%%-%%sj:~6,2%%%%~x1"

> "%temp%1.vbs" ECHO Set fso=Wscript.CreateObject("Scripting.FileSystemObject")
>> "%temp%1.vbs" ECHO set f=fso.opentextfile("c:\1.bat")
>> "%temp%1.vbs" ECHO s=replace(f.readall,"rq", "date")
>> "%temp%1.vbs" ECHO f.close
>> "%temp%1.vbs" ECHO set r=fso.opentextfile("c:\1.bat",2,true)
>> "%temp%1.vbs" ECHO r.write s
>> "%temp%1.vbs" ECHO Wscript.quit

> "%temp%2.vbs" ECHO Set fso=Wscript.CreateObject("Scripting.FileSystemObject")
>> "%temp%2.vbs" ECHO set f=fso.opentextfile("c:\1.bat")
>> "%temp%2.vbs" ECHO s=replace(f.readall,"sj", "time")
>> "%temp%2.vbs" ECHO f.close
>> "%temp%2.vbs" ECHO set r=fso.opentextfile("c:\1.bat",2,true)
>> "%temp%2.vbs" ECHO r.write s
>> "%temp%2.vbs" ECHO Wscript.quit

Start WScript.exe "%temp%1.vbs"
Start WScript.exe "%temp%2.vbs"

Reg.exe add "HKCR\*\shell\namedata" /ve /t REG_SZ /d "添加时间" /f
Reg.exe add "HKCR\*\shell\namedata\command" /ve /t REG_SZ /d "cmd.exe /c c:\1.bat %%1" /f

del "%temp%1.vbs"
del "%temp%2.vbs"


vbs替换文本的笨办法,我也希望有其他高手能教我更好的方法

879792799 发表于 2018-11-16 07:56:33

都市浪子666 发表于 2018-11-15 23:41
>c:\1.bat echo ren%%1"%%~n1-%%date:~0,4%%-%%date:~5,2%%-%%date:~8,2%%-%%time:~0,2%%-%%time:~3,2% ...

666666 谢谢大大! 测试成功!!!{:2_123:}

879792799 发表于 2018-11-16 07:58:26

kli 发表于 2018-11-16 00:44
vbs替换文本的笨办法,我也希望有其他高手能教我更好的方法

测试成功 谢谢kli大大!!呵呵
3#都市浪子666大大的方法也不错的!!很简洁!!

huiyao_zhou 发表于 2018-11-16 09:19:41

不耻下问是最好的请教模式。

kli 发表于 2018-11-16 13:04:30

我一开始也是用3楼的转义符,不过测试的时候没通过,所以才转了方法

但是今天测试时又可以了,不知道ltsc1809什么毛病

holley2008 发表于 2018-11-16 13:40:22

kingshui 发表于 2018-11-16 13:27
来个复杂 的供参考。。。。最近在批量系统迁移,写的备份还原。

对楼主的用more的办法会更好些。

好久不用robocopy 都快忘记了。
页: [1] 2
查看完整版本: 不好意思又来!汗 求助bat自动新建另一个批处理并写入命令