如何在c中运行和修改cmd脚本或文件

时间:2021-05-30 02:29:27
@echo off

set "yourDir=C:\Users\asus\Desktop"

echo:List only files:
for %%a in ("%yourDir%\*") do echo %%~fa

echo:List only directories:
for /d %%a in ("%yourDir%\*") do echo %%~fa

echo:List directories and files in one command:
for /f "usebackq tokens=*" %%a in (`dir /b "%yourDir%\*"`) do echo %yourDir%\%%~a

pause

i have a cmd script and i want to include and run it in a c script

我有一个cmd脚本,我想在c脚本中包含并运行它

and if possible modify it (the cmd script contains variables ).

如果可能的话修改它(cmd脚本包含变量)。

do you have a solution ?

你有解决方案吗 ?

1 个解决方案

#1


2  

UPDATE

UPDATE

You can create a script file on-the-fly in the C program that you will call it from. Note you must handle the characters " and \ and % specially when part of a string literal, by using \" and \\ and %% respectively.

您可以在C程序中即时创建脚本文件,您将从中调用它。请注意,您必须分别使用\“和\\和%%来处理字符串”和\和%,特别是在字符串文字的一部分时。

#include <stdio.h>
#include <stdlib.h>

void fatal(char *msg) {
    printf("%s\n", msg);
    exit (1);
    }

void makebat(FILE *fp, char *dirname) {
    fprintf (fp, "@echo off\n");
    fprintf (fp, "\n");
    fprintf (fp, "set \"%s=C:\\Users\\asus\\Desktop\"\n", dirname);
    fprintf (fp, "\n");
    fprintf (fp, "echo:List only files:\n");
    fprintf (fp, "for %%%%a in (\"%%%s%%\\*\") do echo %%%%~fa\n", dirname);
    fprintf (fp, "\n");
    fprintf (fp, "echo:List only directories:\n");
    fprintf (fp, "for /d %%%%a in (\"%%%s%%\\*\") do echo %%%%~fa\n", dirname);
    fprintf (fp, "\n");
    fprintf (fp, "echo:List directories and files in one command:\n");
    fprintf (fp, "for /f \"usebackq tokens=*\" %%%%a in (`dir /b \"%%%s%%\\*\"`) do echo %%%s%%\\%%%%~a\n", dirname, dirname);
    fprintf (fp, "\n");
    fprintf (fp, "pause\n");
}

int main(int argc, char *argv[]) {
    FILE *fp;
    char *fname = "MyScript.bat";
    if ((fp = fopen(fname, "wt")) == NULL)
        fatal("Cannot open script file");

    makebat(fp, "MyDirectory");

    if (fclose (fp))
        fatal("Cannot close script file");
    //system(fname);
    return(0);
}

Script file generated:

脚本文件生成:

@echo off

set "MyDirectory=C:\Users\asus\Desktop"

echo:List only files:
for %%a in ("%MyDirectory%\*") do echo %%~fa

echo:List only directories:
for /d %%a in ("%MyDirectory%\*") do echo %%~fa

echo:List directories and files in one command:
for /f "usebackq tokens=*" %%a in (`dir /b "%MyDirectory%\*"`) do echo %MyDirectory%\%%~a

pause

#1


2  

UPDATE

UPDATE

You can create a script file on-the-fly in the C program that you will call it from. Note you must handle the characters " and \ and % specially when part of a string literal, by using \" and \\ and %% respectively.

您可以在C程序中即时创建脚本文件,您将从中调用它。请注意,您必须分别使用\“和\\和%%来处理字符串”和\和%,特别是在字符串文字的一部分时。

#include <stdio.h>
#include <stdlib.h>

void fatal(char *msg) {
    printf("%s\n", msg);
    exit (1);
    }

void makebat(FILE *fp, char *dirname) {
    fprintf (fp, "@echo off\n");
    fprintf (fp, "\n");
    fprintf (fp, "set \"%s=C:\\Users\\asus\\Desktop\"\n", dirname);
    fprintf (fp, "\n");
    fprintf (fp, "echo:List only files:\n");
    fprintf (fp, "for %%%%a in (\"%%%s%%\\*\") do echo %%%%~fa\n", dirname);
    fprintf (fp, "\n");
    fprintf (fp, "echo:List only directories:\n");
    fprintf (fp, "for /d %%%%a in (\"%%%s%%\\*\") do echo %%%%~fa\n", dirname);
    fprintf (fp, "\n");
    fprintf (fp, "echo:List directories and files in one command:\n");
    fprintf (fp, "for /f \"usebackq tokens=*\" %%%%a in (`dir /b \"%%%s%%\\*\"`) do echo %%%s%%\\%%%%~a\n", dirname, dirname);
    fprintf (fp, "\n");
    fprintf (fp, "pause\n");
}

int main(int argc, char *argv[]) {
    FILE *fp;
    char *fname = "MyScript.bat";
    if ((fp = fopen(fname, "wt")) == NULL)
        fatal("Cannot open script file");

    makebat(fp, "MyDirectory");

    if (fclose (fp))
        fatal("Cannot close script file");
    //system(fname);
    return(0);
}

Script file generated:

脚本文件生成:

@echo off

set "MyDirectory=C:\Users\asus\Desktop"

echo:List only files:
for %%a in ("%MyDirectory%\*") do echo %%~fa

echo:List only directories:
for /d %%a in ("%MyDirectory%\*") do echo %%~fa

echo:List directories and files in one command:
for /f "usebackq tokens=*" %%a in (`dir /b "%MyDirectory%\*"`) do echo %MyDirectory%\%%~a

pause