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-07-01 08:51
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Using a batch file to get the current username, then replace text in a file? View 1,357 Replies 11
Original Poster Posted 2010-04-17 07:30 ·  中国 北京 鹏博士BGP
新手上路
Credits 13
Posts 13
Joined 2010-04-16 21:35
16-year member
UID 164766
Gender Male
Status Offline
Question: how can I use a DOS batch file to get the current username of the XP system, and then replace administrator in MirrorDir.ini?
The commands I know so far are:
echo %username% display the current username
echo %username%>a.txt append the displayed result to a.txt

But how can I use a batch file to replace administrator in MirrorDir.ini with the current XP system username?

Requirement: as soon as the batch file is run, it should immediately modify MirrorDir.ini.
I don't want to use other software to do it, many thanks.

The contents of MirrorDir.ini are as follows:

LastTask=0

MainDir=C:\Documents and Settings\Administrator\Desktop
ImageDir=d:\0beifen
MainDirHistory=C:\Documents and Settings\Administrator\Desktop
ImageDirHistory=d:\0beifen
Excludes=
StartupRun=1
DeleteOthers=1

[ Last edited by ygqiang on 2010-4-17 at 07:41 ]
Floor 2 Posted 2010-04-17 07:40 ·  中国 北京 鹏博士BGP
新手上路
Credits 13
Posts 13
Joined 2010-04-16 21:35
16-year member
UID 164766
Gender Male
Status Offline
Floor 3 Posted 2010-04-17 07:41 ·  中国 北京 鹏博士BGP
新手上路
Credits 13
Posts 13
Joined 2010-04-16 21:35
16-year member
UID 164766
Gender Male
Status Offline
The main issue is:

StartupRun=
DeleteOthers=

In these 2 places, the 1 after them was not added.

It seems this echo command can't add the number 1 into the file?

[ Last edited by ygqiang on 2010-4-17 at 07:45 ]
Floor 4 Posted 2010-04-17 07:46 ·  中国 北京 鹏博士BGP
新手上路
Credits 13
Posts 13
Joined 2010-04-16 21:35
16-year member
UID 164766
Gender Male
Status Offline
Also, the method I'm using is a bit complicated.

Isn't there any other simpler method?
Floor 5 Posted 2010-04-17 15:58 ·  中国 北京 鹏博士BGP
新手上路
Credits 13
Posts 13
Joined 2010-04-16 21:35
16-year member
UID 164766
Gender Male
Status Offline
Everyone, is there no solution?

Many thanks
Floor 6 Posted 2010-04-17 16:15 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Floor 7 Posted 2010-04-17 16:20 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Or
@echo off&setlocal enabledelayedexpansion
for /F "delims=" %%a in (MirrorDir.ini) do set a=%%a&set "a=!a:Administrator=%username%!"&>>hanye.txt echo.!a!
del /q /f MirrorDir.ini
ren hanye.txt MirrorDir.ini

Or directly use third-party commands such as sed or fr, for example
fr MirrorDir.ini -fic:Administrator -t:%username%


[ Last edited by Hanyeguxing on 2010-4-17 at 16:25 ]
Floor 8 Posted 2010-04-17 21:12 ·  中国 北京 鹏博士BGP
新手上路
Credits 13
Posts 13
Joined 2010-04-16 21:35
16-year member
UID 164766
Gender Male
Status Offline
Many thanks to the poster above.

I've already found a solution for my method.

echo [CommonSetting]>MirrorDir.ini
echo LastTask=0>>MirrorDir.ini
echo [新任务1]>>MirrorDir.ini
echo MainDir=%userprofile%\桌面>>MirrorDir.ini
echo ImageDir=d:\0beifen>>MirrorDir.ini
echo MainDirHistory=%userprofile%\桌面>>MirrorDir.ini
echo ImageDirHistory=d:\0beifen>>MirrorDir.ini
echo Excludes=>>MirrorDir.ini
echo StartupRun=1 1>>MirrorDir.ini
echo DeleteOthers=1 1>>MirrorDir.ini
Floor 9 Posted 2010-04-17 21:47 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
echo DeleteOthers=1 1>>MirrorDir.ini
write it as
>>MirrorDir.ini echo DeleteOthers=1
Floor 10 Posted 2010-04-17 21:57 ·  中国 北京 鹏博士BGP
新手上路
Credits 13
Posts 13
Joined 2010-04-16 21:35
16-year member
UID 164766
Gender Male
Status Offline
Many thanks to the poster above.

I tried it.

echo DeleteOthers=1 1>>MirrorDir.ini
written as
>>MirrorDir.ini echo DeleteOthers=1

These two actually have the same effect in actual use. No need to modify it.
Floor 11 Posted 2010-04-17 21:58 ·  中国 北京 鹏博士BGP
新手上路
Credits 13
Posts 13
Joined 2010-04-16 21:35
16-year member
UID 164766
Gender Male
Status Offline
This is the software I ultimately want: timed backup of desktop files
Floor 12 Posted 2010-04-18 00:48 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
1, when written as echo.%var%>>a.txt, the last character in %var% must not be a digit
2, so adding a space like echo.%var% >>a.txt solves it, but this will cause an extra space at the end of each line
3, >>a.txt echo.%var% means you don't need to worry whether the last character of %var% is a digit, and there won't be an extra space at the end of the line either
4, your echo.%var% 1>>a.txt is actually just echo.%var% >>a.txt
Forum Jump: