Board logo

标题: [文件操作] 批处理保存文件到桌面的代码怎样写? [打印本页]

作者: xzwcn    时间: 2012-4-2 19:37     标题: 批处理保存文件到桌面的代码怎样写?

如果我想写一个批处理,用来保存电脑的IP.【用于多台电脑】操作的。

win7及xp下的代码是否相同?

有点点思路,但不知怎样写全。。

如在我的电脑上可以用:

ipconfig/all >C:\Users\”我的用户名"\Desktop\ip2.txt

但在其它电脑,用户名是不同的,该怎样操作呢?

请帮忙想下,谢谢。
作者: ivor    时间: 2012-4-2 20:03

如果批处理放到桌面,就不用考虑系统了
作者: QIAOXINGXING    时间: 2012-4-2 20:03

本帖最后由 QIAOXINGXING 于 2012-4-2 20:07 编辑

xp下获取桌面路径,win7应该也可以吧。。。。
  1. @echo off
  2. for /f "skip=4 tokens=2,*" %%i in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop"') do  (echo  %%~j)
  3. pause
复制代码

作者: ivor    时间: 2012-4-2 20:08

  1. userprofile
复制代码

作者: xzwcn    时间: 2012-4-2 20:14

回复 3# QIAOXINGXING


    请问这个是怎样用的?
作者: xzwcn    时间: 2012-4-2 20:14

回复 2# ivor


    具体怎样操作呢?
作者: find    时间: 2012-4-2 20:49

回复 5# xzwcn


那个是直接用的
作者: xzwcn    时间: 2012-4-2 21:30

回复 7# find


    没反应的。。。
作者: find    时间: 2012-4-2 22:47

回复 8# xzwcn


把@echo off删掉,看看是个什么情况。
作者: apang    时间: 2012-4-3 00:18

  1. @echo off
  2. if exist "%userprofile%\desktop\" ipconfig /all>"%userprofile%\desktop\ip2.txt"
  3. if exist "%userprofile%\桌面\" ipconfig /all>"%userprofile%\桌面\ip2.txt"
复制代码

作者: find    时间: 2012-4-3 09:14

  1. @echo off
  2. if exist "%userprofile%\desktop\" (
  3.     ipconfig /all>"%userprofile%\desktop\ip2.txt"
  4. ) else if exist "%userprofile%\桌面\" (
  5.     ipconfig /all>"%userprofile%\桌面\ip2.txt"
  6. )
复制代码

作者: xzwcn    时间: 2012-4-3 12:56

回复 11# find


    你好!为什么不能获取IP的?保存了一个空白文本到桌面。。
作者: find    时间: 2012-4-3 13:04

回复 12# xzwcn


把@echo off删掉,看看是个什么情况。
作者: xzwcn    时间: 2012-4-3 17:39

回复 13# find


    谢谢提醒!可以了。
如果我想保存出来的文件是以本机用户命名的,那么该怎样修改代码呢?请指导~
作者: apang    时间: 2012-4-3 17:57

回复 14# xzwcn

将代码中的ip2.txt都改成%username%.txt
作者: find    时间: 2012-4-3 20:48

回复 14# xzwcn


在命令行窗口里面运行一下set,你会看到很多系统变量的。




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2