[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

TOP

回复 8# idctop

在 7 楼基础上加入了 mysql 导入代码, 注意 BAT 文件保存为 ANSI 编码, 经测试 "[标签:内容]这样内容整行" 是完全可以正常过滤掉的.

这一部分的字母大小写不要修改
  1. mysql -h127.0.0.1 -P3306 -uroot -p123456 -Dgupiao -e
复制代码
完全代码
  1. @if(0)==(0) echo off
  2. set str="code,name,description,market,suggestion,closing_price,price,exchange rate,price earnings ratio,the main cost,institutional participation"
  3. dir /b *.txt | cscript -nologo -e:jscript %0 %str%
  4. for /f "delims=" %%a in ('dir /b *.txt') do (
  5. echo %%a
  6. mysql -h127.0.0.1 -P3306 -uroot -p123456 -Dgupiao -e "LOAD DATA LOCAL INFILE '%%a' INTO TABLE `sszjlx` FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (`code`, `name`, `description`, `market`, `suggestion`, `closing_price`, `price`, `exchange rate`, `price earnings ratio`, `the main cost`, `institutional participation`);"
  7. )
  8. pause & exit
  9. @end
  10. var fso = new ActiveXObject("Scripting.FileSystemObject");
  11. var s = WScript.Arguments.Item(0);
  12. var re = new RegExp("^" + s, "i");
  13. var reg = /.*\[标签:内容\].*/g;
  14. var fso = new ActiveXObject("Scripting.FileSystemObject");
  15. while (!WScript.StdIn.AtEndOfStream){
  16.     var filename = WScript.StdIn.Readline();
  17.     var text = adoLoadText(filename, 'UTF-8').replace(/\r?\n/ig,'');
  18.     while (!(re.test(text))){
  19.        text = s + "\r\n" + text.replace(/&sbquo/ig,',').replace(/","/ig,'\r\n').replace(reg,'');
  20.        TextToFile(text, filename);
  21.     }
  22. }
  23. function adoLoadText(filename, charset) {
  24.     var fs, text;
  25.     fs = new ActiveXObject("ADODB.Stream");
  26.     fs.type = 2;
  27.     fs.charset = charset;
  28.     fs.open();
  29.     fs.loadFromFile(filename);
  30.     text = fs.readText(-1);
  31.     fs.close();
  32.     return text;
  33. }
  34. function TextToFile(text, filename) {
  35.     var fs = new ActiveXObject("Adodb.Stream");
  36.     fs.Charset = 'UTF-8';
  37.     fs.Open();
  38.     fs.WriteText(text);
  39.     fs.SaveToFile(filename, 2);
  40.     fs.Close();
  41. }
复制代码

TOP

本帖最后由 aa77dd@163.com 于 2015-6-11 19:33 编辑

回复 14# idctop

你没有安装 mysql 吗? 或者 path 变量里没有加入对应的目录

mysql 下载地址:
http://dev.mysql.com/get/Downloads/MySQLInstaller/mysql-installer-community-5.6.25.0.msi

那么你就把 mysql 安装并且找到 mysql.exe 所在的目录:
假定你安装了 5.6 版, 并且安装时用的默认路径, 那么会是如下的:
对 32 位 XP, WIN7:
  1. "%ProgramFiles%\MySQL\MySQL Server 5.6\bin\mysql.exe"
复制代码
对 64 位 WIN7:
  1. "%ProgramFiles(x86)%\MySQL\MySQL Server 5.6\bin\mysql.exe"
复制代码
弄清那个全路径是怎样的, 并且用它替换 第 7 行开头的 mysql   注意如果路径中有空格, 那么必须用双引号对括起来

TOP

本帖最后由 aa77dd@163.com 于 2015-6-11 19:46 编辑

回复 14# idctop

可能你改错了, 你把修改的代码贴出来看看
  1. -h127.0.0.1 -P3306 -uroot -p123456 -Dgupiao
复制代码
这里的这些参数中

-p123456    已经指定了密码, 不会要输入密码的,  -p后是不跟空格的, 你是不是用了给加了空格了, 成了
-p 123456

TOP

回复 18# idctop


" -h127.0.0.1 -P3306 -uroot -p123456 -Dgupiao -e "

上面的这部分(不包括红色的引号), 严格的按我给你的处理, 不要修改大小写! 不要另加空格!

TOP

本帖最后由 aa77dd@163.com 于 2015-6-11 20:54 编辑

回复 21# idctop
  1. (`code`, `name`, `description`, `market`, `suggestion`, `closing_price`, `price`, `exchange rate`, `price earnings ratio`, `the main cost`, `institutional participation`)
复制代码
指定了导入的数据是要插入到哪些字段, 每行数据都会按顺序和这些字段对应,
id 没有在上面指定, 是不会有影响的, 它是自动增长的, 也不能由数据给出, 只能由系统自动处理.
如果一个输入行含有过多的字段,则多余的字段被忽略,并且警告的数量增加。
如果一个输入行含有的字段过少,则输入字段缺失的表中的列被设置为默认值。
如果字段值缺失,则对一个空字段值会被按不同方式理解:
·         对于字符串类型,列被设置为空字符串。
·         对于数字类型,列被设置为0。
·         对于日期和时间类型,列被设置为该类型相应的“zero”。

你给出的样本数据中, 有很多行数据是有缺失的, 如果在 mysql 控制台, 你就会看到它们产生的警告信息

If an input line has too many fields, the extra fields are ignored and the number of warnings is incremented.

If an input line has too few fields, the table columns for which input fields are missing are set to their default values. Default value assignment is described in Section 11.6, “Data Type Default Values”.

An empty field value is interpreted different from a missing field:

For string types, the column is set to the empty string.

For numeric types, the column is set to 0.

For date and time types, the column is set to the appropriate “zero” value for the type. See Section 11.3, “Date and Time Types”.


http://dev.mysql.com/doc/refman/5.6/en/load-data.html

TOP

回复 24# idctop

在 18 楼代码里新增两行
  1. if not exist done md done
  2. move /y *.txt done\
复制代码
放在第 8 行
  1. pause & exit
复制代码
的上面

被处理过的文件会被转移到 done\ 目录中, 不会被再次处理

关于只导入了 1000 行数据, 可能是数据库的配置上对表的大小作了限制
1

评分人数

    • idctop: 不会重复导入了,根据你的提示,我检查了下 ...技术 + 1

TOP

回复 23# idctop

可以用下面的批处理代码 检查下 表  zuoriqgqp   的配置信息, 看有没有相关的限制
  1. @echo off
  2. if exist "d:\tab_info" del /f /s /q "d:\tab_info"
  3. "%ProgramFiles%\MySQL\MySQL Server 5.6\bin\mysql.exe" -h127.0.0.1 -P3306 -uroot -p123456 -Dgupiao -e "SELECT 'TABLE_NAME','ENGINE','AVG_ROW_LENGTH','MAX_DATA_LENGTH','CREATE_OPTIONS' UNION ALL select TABLE_NAME, ENGINE, AVG_ROW_LENGTH, MAX_DATA_LENGTH, CREATE_OPTIONS from information_schema.`TABLES` where table_schema = 'gupiao' and table_name = 'zuoriqgqp' INTO OUTFILE 'd:/tab_info';"
  4. type "d:\tab_info"
  5. pause
  6. exit
复制代码

TOP

本帖最后由 aa77dd@163.com 于 2015-6-12 18:53 编辑

回复 27# idctop

经测试的完全代码, 注意包括原来的 jscript 代码也在多处作了修改, mysql 改成你自己的全路径即可了
  1. @if(0)==(0) echo off
  2. set str="code,name,description,market,suggestion,closing_price,price,exchange rate,price earnings ratio,the main cost,institutional participation"
  3. dir /b *.txt | cscript -nologo -e:jscript %0 %str%
  4. set "mysql=%ProgramFiles(x86)%\MySQL\MySQL Server 5.6\bin\mysql.exe"
  5. set "server=127.0.0.1"
  6. set "port=3306"
  7. set "username=root"
  8. set "userpwd=123456"
  9. set "dbname=gupiao"
  10. set "tabname=zuoriqgqp"
  11. REM 下行代码为 表 %tabname% 新增两个字段 riqi 和 shijian, 如果字段还没有建立, 就运行一次, 之后就不要再运行了.
  12. rem "%mysql%" -h%server% -P%port% -u%username% -p%userpwd% -D%dbname% -e "ALTER TABLE `%tabname%` ADD COLUMN (`riqi` DATE, `shijian` TIME);"
  13. for /f "delims=" %%a in ('dir /b *.txt') do (
  14.   echo %%a
  15.   "%mysql%" -h%server% -P%port% -u%username% -p%userpwd% -D%dbname% -e "LOAD DATA LOCAL INFILE '%%a' INTO TABLE `%tabname%` FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (`riqi`, `shijian`, `code`, `name`, `description`, `market`, `suggestion`, `closing_price`, `price`, `exchange rate`, `price earnings ratio`, `the main cost`, `institutional participation`);"
  16. )
  17. if not exist done md done
  18. move /y *.txt done\
  19. pause & exit
  20. @end
  21. var fso = new ActiveXObject("Scripting.FileSystemObject");
  22. var s = WScript.Arguments.Item(0);
  23. var re = new RegExp("^" + s, "i");
  24. var reg = /.*\[标签:内容\].*/g;
  25. var fso = new ActiveXObject("Scripting.FileSystemObject");
  26. var now = new Date();
  27. var s_date = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();
  28. var s_time = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
  29. var time_source = 'file_name'; // 值为 'file_name' : 从数据文件名获取时间;  否则, 获取系统当前时间;
  30. while (!WScript.StdIn.AtEndOfStream){
  31.     var filename = WScript.StdIn.Readline();
  32.     if (time_source == 'file_name') {
  33.       var s_dt = filename.replace(/股票数据|\.txt/gi, '').split(' ');
  34.       s_date = s_dt[0];
  35.       s_time = s_dt[1].replace(/-/g, ':');
  36.     }
  37.     var text = adoLoadText(filename, 'UTF-8').replace(/\r?\n/ig,'');
  38.     while (!(re.test(text))){
  39.        text = s + "\r\n" + s_date + ',' + s_time + ',' + text.replace(/&sbquo/ig,',').replace(/","/ig,'\r\n' + s_date + ',' + s_time + ',').replace(reg,'');
  40.        TextToFile(text, filename);
  41.     }
  42. }
  43. function adoLoadText(filename, charset) {
  44.     var fs, text;
  45.     fs = new ActiveXObject("ADODB.Stream");
  46.     fs.type = 2;
  47.     fs.charset = charset;
  48.     fs.open();
  49.     fs.loadFromFile(filename);
  50.     text = fs.readText(-1);
  51.     fs.close();
  52.     return text;
  53. }
  54. function TextToFile(text, filename) {
  55.     var fs = new ActiveXObject("Adodb.Stream");
  56.     fs.Charset = 'UTF-8';
  57.     fs.Open();
  58.     fs.WriteText(text);
  59.     fs.SaveToFile(filename, 2);
  60.     fs.Close();
  61. }
复制代码
1

评分人数

    • idctop: 强大的批处理啊,强人!技术 + 1

TOP

返回列表